?
Solved

2 level sort on array of hashes

Posted on 2003-03-03
2
Medium Priority
?
415 Views
Last Modified: 2010-08-05
I have an array of hashes, each hash contains several numeric values, some of which are null strings. Note that this means no value recorded, not 0.

name             1 yr 3 yr 5 yr
-------------------------------
ACAST - MIX 0    4.60 5.53  
ACAST - MIX 30   1.30 6.06  
ACAST - MIX 30   1.30 5.63 6.96
ACAST - MIX 50  -1.60 5.45 7.57
ACAST - MIX 70  -3.60 5.65 8.19

I want results returned sorted firstly by one key, but also with a secondary sort on a second key. For example sorted by 1 yr and then 3 yr (within the 1 yr results), or

ACAST - MIX 30   1.30 5.63 6.96
ACAST - MIX 30   1.30 6.06  

ozo came up with this great one liner for a primary sort. How to adapt it to a 2 level sort?
 
@sorted_amounts = sort{ ($a->{$key}||0) <=> ($b->{$key}||0) } @unsorted_amounts;
 
0
Comment
Question by:Kim Ryan
2 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 1200 total points
ID: 8060869
@sorted_amounts = sort{ ($a->{yr1}||0) <=> ($b->{yr1}||0) || ($a->{yr3}||0) <=> ($b->{yr3}||0) } @unsorted_amounts;
0
 
LVL 19

Author Comment

by:Kim Ryan
ID: 8069927
thanks ozo, works perfectly
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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
Suggested Courses

621 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