UPDATE: Erk. No we didnt. /me removes lenscap from eyes. Read Masems reply to my post below. OTOH introducing the ST is never a bad thing, just perhaps not the most appropriate thing ;-)
My interpretation is that you want to sort the list, but be able to determine the elements original positions afterwards. The way to do this is a twist on the Schwartzian Transform.
Basically the idea being that you transform the list into an intermediate form, one that holds the meta info you need, sort as normal and then either use the meta version and or unpack it into the sorted form.
I took the liberty of making the list be letters so that the output would be clearer. It need not be so by simply changing the 'cmp' into a '<=>'. Actually on that thought you didnt need the int in your sort routine (perl almost always handles those kind of things in a DWIM fashion)use strict; use warnings; my @array=qw(Q W E R T Y U I O P); my @sorted_meta=sort { $a->[0] cmp $b->[0] } map { [ $array[$_] , $_ ] + } 0..$#array; my @sorted=map { $_->[0] } @sorted_meta; local $,=" "; local $\="\n"; print "Orig: ",@array; print "Meta: ",map { ("[",@{$_},"]") } @sorted_meta; print "Sort: ",@sorted; __END__ Outputs: Orig: Q W E R T Y U I O P Meta: [ E 2 ] [ I 7 ] [ O 8 ] [ P 9 ] [ Q 0 ] [ R 3 ] [ T 4 ] [ U 6 ] + [ W 1 ] [ Y 5 ] Sort: E I O P Q R T U W Y
Anyway, hope one of us has managed to answer the question you were asking.. :-)
Yves / DeMerphq
--
When to use Prototypes?
In reply to Re: How do I record in what order a sort was preformed
by demerphq
in thread How do I record in what order a sort was preformed
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |