Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Easy Sorting Question

Posted on 1999-07-22
4
Medium Priority
?
161 Views
Last Modified: 2010-03-04
I'm sure this question has been asked and answered hundreds of times on EE, but in the time it would take to find it I can just ask it again...

I have a hash called players.  The keys are simply integers numbering the players.  The values are as follows:

$players{key}{name}
$players{key}{score}
$players{key}{losses}
.
.
.
etc.

I need to output a list to the user with all players and their corresponding values listed, but I want the list sorted by score.  I've written a bunch of simple sorts that are close, but they are not getting the job done.

Thanks,

Brian
0
Comment
Question by:BSwabby
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
helver earned 120 total points
ID: 1214339
sub byscore {
    $players{$a}{score} <=> $players{$b}{score};
}

@sorted_keys_list = sort byscore (keys %players);
0
 

Author Comment

by:BSwabby
ID: 1214340
Hi Helver-

Thanks for the code; it's exactly right.  The sick thing is I had the EXACT same sort code, but am having trouble printing the players and scores out of sorted_keys_list.  Any ideas on this one?  (I know it's rude to ask you a second question, but you're getting the points either way-- don't worry...)
0
 
LVL 84

Expert Comment

by:ozo
ID: 1214341
foreach( @sorted_keys_list ){
  print "@{$players{$_}}{'name','score'}\n";
}
0
 

Author Comment

by:BSwabby
ID: 1214342
Thanks to you both.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question