in reply to Re: Sorting array
in thread Sorting array
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Sorting array
by frozenwithjoy (Priest) on Jun 15, 2013 at 00:34 UTC | |
|
Re^3: Sorting array
by rkk (Novice) on Jun 15, 2013 at 02:48 UTC | |
by NetWallah (Canon) on Jun 15, 2013 at 02:57 UTC | |
by rkk (Novice) on Jun 15, 2013 at 02:55 UTC | |
|
Re^3: Sorting array
by rkk (Novice) on Jun 14, 2013 at 23:57 UTC |