Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Re: Stable sorting in Perl (old)

by wirrwarr (Monk)
on Aug 28, 2003 at 07:13 UTC ( [id://287266]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Stable sorting in Perl (old)
in thread Stable sorting in Perl

Sorry, maybe I just don't get it, but why is your first example a stable sort?
my @unsorted = ([1,1], [2,1], [1,2], [2,2], [2,3], [1,3], [2,4], [1,4]); print join( ' ', map { $_->[0].$_->[1] } sort { $a->[0] <=> $b->[0] } @unsorted) . "\n"; my @sortedindices = sort { $unsorted[$a]->[0] <=> $unsorted[$b]->[0] } 0..$#unsorted; print join ( ' ', map { $_->[0].$_->[1] } @unsorted[@sortedindices]); __END__ 11 12 13 14 23 21 24 22 11 12 13 14 23 21 24 22
Both sorts are not stable.

daniel.

update: got it now ;-) replace compare-function by
$unsorted[$a]->[0] <=> $unsorted[$b]->[0] || $a <=> $b

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-16 05:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found