[LUNI] Sorting algorithm in perl?

Joel F. Hacker hacker at cig.mot.com
Fri Mar 22 22:36:02 CST 2002


If I have an array of numbers that looks like this:

38|2028|0|3|5|7|0|0|0|2106636|2|0|
43|2034|0|3|5|7|0|0|0|2106686|2|0|
48|2040|0|3|5|7|0|0|0|2106747|2|0|
28|1800|0|3|5|7|0|0|0|2099058|2|0|
33|1806|0|3|5|7|0|0|0|2099074|2|0|
38|1812|0|3|5|7|0|0|0|2099105|2|0|
43|1818|0|3|5|7|0|0|0|2099155|2|0|

and if I run sort like this on the array,

sub sort_array
{
   my @in = @_;
   my @sorted = sort { ${$a}[0] cmp ${$b}[0] } @in;
}


I get this:

28|1800|0|3|5|7|0|0|0|2099058|2|0|
33|1806|0|3|5|7|0|0|0|2099074|2|0|
38|2028|0|3|5|7|0|0|0|2106636|2|0|
38|1812|0|3|5|7|0|0|0|2099105|2|0|
43|2034|0|3|5|7|0|0|0|2106686|2|0|
43|1818|0|3|5|7|0|0|0|2099155|2|0|
48|2040|0|3|5|7|0|0|0|2106747|2|0|

BUT, my I also need to have the second
column sorted after the first...
notice that the second columns within
38 & 43 of A[0] are out of order...

Anyone have any ideas?

Thanks!

Joel




More information about the luni mailing list