Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Sort not acting as expected on Hash Refby oakbox (Chaplain) |
on Jan 01, 2003 at 11:18 UTC ( [id://223561]=perlquestion: print w/replies, xml ) | Need Help?? |
oakbox has asked for the wisdom of the Perl Monks concerning the following question:
Sort isn't acting like I expect it to and I'm a little confused. I have a hash reference that looks like this:
I want to reverse sort on element_one (numerically) and then sort normally on element_two. Here is an example of my data set:
I want to take that information and force it into an order like this:
My first crack at this was using a sort statement like so:
But this gives me an incorrect order. Instead of giving me an order like this: 2, 1, 1, 1, 0 it gives me something like 0, 2, 1, 1, 1. I could not get this straightened out, so eventually ended up substituting this line: I'm even more confused because the second sort using the second value works AS EXPECTED: My question is, why doesn't the sort on the hash ref give me the expected behaviour? Is there a problem with the way I am expanding my $sort_line_contents reference? I see that it is reading out the correct VALUES, it is just putting them in an unexpected order. I get the same unexpected order with the <=> and the cmp operators???
Back to
Seekers of Perl Wisdom
|
|