Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2018-07-19 13:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (408 votes). Check out past polls.

    Notices?