in reply to Sorting Index of An Array
Use a Schwartzian transform - like this: (update: but see salva's comment below for a better approach)
@sorted = map {$_->[0]} map { $i=0; sort {$b->[1]<=>$a->[1]} map {[$i++,$_]} splice (@s +im,0,9) } 0..8; print "\@index_ordered =(\n"; for(0..8) { print " " x 4, join(', ', splice (@sorted,0,9) ),"\n"; } print ");\n";
Output:
@index_ordered =( 0, 1, 3, 2, 4, 5, 7, 8, 6 1, 0, 3, 8, 2, 4, 7, 6, 5 2, 3, 8, 0, 1, 4, 7, 5, 6 3, 0, 1, 2, 8, 7, 6, 4, 5 4, 8, 7, 0, 1, 2, 6, 3, 5 5, 0, 7, 1, 2, 3, 6, 8, 4 6, 7, 8, 3, 4, 1, 2, 0, 5 7, 6, 8, 3, 4, 0, 1, 5, 2 8, 7, 6, 3, 4, 1, 2, 0, 5 );
For equal elements in each row of @sim the order of their indices is undetermined. Note that this is destructive to @sim - it will be empty after the sort.
While the above code DWYM, you would have been better off having an array of arrays in the first place.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sorting Index of An Array
by salva (Canon) on May 14, 2007 at 12:08 UTC | |
by shmem (Chancellor) on May 14, 2007 at 13:18 UTC |