in reply to Re: Sorting array
in thread Sorting array
That doesn't quite give the output the OP requested, and will fail for multi-digit "ch.." values.
If the entries always start with "ch", this code should work:
Note - OP's data has been modified slightly - adding a 2-digit "ch" value ("ch11").perl -e 'my @arr = ( [ ch1=> 100 ], [ ch2=> 20 ], [ ch11=> 13 ], [ ch2 +=> 45 ], [ ch1=> 1 ] ); @s= sort( { substr($arr[$a]->[0],2) <=> sub +str($arr[$b]->[0],2) or $arr[$a]->[1] <=> $arr[$b]->[1]} 0..$#arr);pr +int qq|@s\n|' #Output: #4 0 1 3 2
Also - this is not the most efficient sort for this kind of data, but will work well for small to medium size data. More complex transforms are required for efficiency with large amounts of data (For some definition of 'large').
"The trouble with the Internet is that it's replacing masturbation as a leisure activity."
-- Patrick Murray
In Section
Seekers of Perl Wisdom