Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Re: Stable sorting in Perl (old)

by wirrwarr (Monk)
on Aug 28, 2003 at 07:13 UTC ( #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
Node Status?
node history
Node Type: note [id://287266]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2018-12-14 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many stories does it take before you've heard them all?







    Results (64 votes). Check out past polls.

    Notices?