Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Sorting Index of An Array

by salva (Canon)
on May 14, 2007 at 12:08 UTC ( [id://615284]=note: print w/replies, xml ) Need Help??


in reply to Re: Sorting Index of An Array
in thread Sorting Index of An Array

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

Replies are listed 'Best First'.
Re^3: Sorting Index of An Array
by shmem (Chancellor) on May 14, 2007 at 13:18 UTC
    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}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://615284]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-19 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found