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


Easy Sorting Question

Posted on 1999-07-22
Medium Priority
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:


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.


Question by:BSwabby
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

Accepted Solution

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

@sorted_keys_list = sort byscore (keys %players);

Author Comment

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...)
LVL 84

Expert Comment

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

Author Comment

ID: 1214342
Thanks to you both.

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 (…
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