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
    Use a Schwartzian transform

    To sort lists of nine elements, applying the ST is probably counterproductive. And anyway, for that particular problem, using a Orcish Maneuver would be more suitable:

    my @index_sorted = map { my @row = splice @sim, 0, N; sort { $row[$b] <=> $row[$a] } 0..(N-1); } 0..(M-1);
      Just a nit - while your approach is far better than mine and less convoluted, there's no OR and no cache, so there's no Orcish (or-cache) Maneuver. That would be (from Shlomo Yona's Perl lectures 2002):

      The Orcish Maneuver -- example

      keys my %or_cache = @in; @out = sort { ($or_cache{$a} ||= KEY($a)) cmp ($or_cache{$b} ||= KEY($b)) } @in;

      The first part of the sortsub sees if the sortkey for $a is cached.

      If not, it extracts the value of the sortkey from the operand and caches it.

      The sortkey for $a is then compared to the sortkey for $b.

      Anyways, salva++ for simplifying things :-)

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