For yucks, I wrote a simple Perl script to demostrate the different sort "pragmas" and their effect on perserving (or not) the existing order of elements in an array.   I had to add a few more elements to the test array to demonstrate the effect.

Regards,

... Dewey


-----CODE----

use strict;


my @nsarray = (
                {name => 'e', status => 5}, {name => 'f', status => 5},
                {name => 'a', status => 1}, {name => 'd', status => 1},
                {name => 'b', status => 2}, {name => 'c', status => 2},
                {name => 'g', status => 4}, {name => 'h', status => 4},
                {name => 'j', status => 3}, {name => 'i', status => 3},
                {name => 'y', status => 0}, {name => 'x', status => 0},
               
                 );


print "not sorted - array element order\n";

foreach my $ns (@nsarray) {
        print "name => '", $ns->{'name'}, "', status = '", $ns->{'status'}, "'\n";
}

print "\nquicksort - not stable\n";
{
        eval 'use sort qw(defaults _quicksort)';
        eval 'no sort "stable"';
       
        print "current sort => '", sort::current(), "'\n";
       
        foreach my $ns (sort {$a->{'status'} <=> $b->{'status'}} @nsarray) {
                print "name => '", $ns->{'name'}, "', status = '", $ns->{'status'}, "'\n";
        }
        eval 'use sort "defaults"';
       
}


print "\nuse 'stable'\n";
{
        eval 'use sort "stable"';
       
        print "current sort => '", sort::current(), "'\n";
       
        foreach my $ns (sort {$a->{'status'} <=> $b->{'status'}} @nsarray) {
                print "name => '", $ns->{'name'}, "', status = '", $ns->{'status'}, "'\n";
        }
        eval 'use sort "defaults"';
       
}


print "\nuse '_mergesort'\n";
{
        eval 'use sort "_mergesort"';
       
        print "current sort => '", sort::current(), "'\n";
       
        foreach my $ns (sort {$a->{'status'} <=> $b->{'status'}} @nsarray) {
                print "name => '", $ns->{'name'}, "', status = '", $ns->{'status'}, "'\n";
        }
        eval 'use sort "defaults"';
}

------ END-----



----- Forwarded by D D Allen/Fairfax/IBM on 05/05/2006 02:51 PM -----

D D Allen/Fairfax/IBM wrote on 05/05/2006 12:44:02 PM:

> The documentation you're looking for is available at this URL.

>
> http://search.cpan.org/~nwclark/perl-5.8.8/lib/sort.pm

>
> Read the Description section -- particularly the parts about "stablility".

>
> There are two solutions.

>
> A:  use sort 'stable';

>
> B:  sort on both status and name as follows.

>
> sort { $a->{'status'} <=> $b->{'status'} || $a->{'name'} cmp $b->{'name'} }

>
> Both solutions should produce:

>
> {name => 'a', status => 0}, {name => 'd', status => 0}, {name =>
> 'b', status => 1}, {name => 'c', status => 1}

>
>
> Regards,
>
> ... Dewey
>

>
> [EMAIL PROTECTED] wrote on
> 05/05/2006 11:12:22 AM:
>
> > Hello all,

> >  
> > I can't find anything in the doc about whether Perl's sort function
> > maintaining the order in equivalent subsequences. Suppose I have an
> > array of hashrefs that looks like this:

> >  
> > {name => 'a', status => 0}, {name => 'b', status => 1}, {name =>
> > 'c', status => 1}, {name => 'd', status => 0}

> >  
> > and I sort { $a->{'status'} <=> $b->{'status'} }
> >  
> > Should I expect the result (order of the names) to always be ('a',
> > 'd', 'b', 'c'), or could it be, for example, ('d', 'a', 'b', 'c')
> > (i.e., could two entries with the same status value change relative order)?

> >  
> > Joe
> >  
> > Joseph Discenza, Senior Programmer/Analyst
> > mailto:[EMAIL PROTECTED]
> >
> > Carleton Inc. http://www.carletoninc.com
> > 574.243.6040 ext. 300
> > Fax: 574.243.6060_______________________________________________
> > Perl-Win32-Users mailing list
> > [email protected]
> > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to