2 level sort on array of hashes

Posted on 2003-03-03
Medium Priority
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;
Question by:Kim Ryan
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
LVL 84

Accepted Solution

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

Author Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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

770 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