Well, it would appear Masem and I have interpreted the question differently.

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.

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
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)

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.