Dr. Mu has asked for the wisdom of the Perl Monks concerning the following question:
Does sort come with any documented guarantee that the original order of "equal" items will be preserved? If so, can this behavior be counted on to persist from one version of Perl to the next? If not, is there a better way to do so than the following?
where &foo returns the field or aspect of each item being sorted on (and where a Schwartzian Transform would be performed if @orig is sufficiently long and &foo is sufficiently complicated).@sorted = sort {&foo($orig[$a]) cmp &foo($orig[$b]) || $a <=> $b} (0 . +. @orig - 1)
(The best answer will end up as a margin note in my camel book.)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: The Effect of sort on Equal Items
by BrowserUk (Patriarch) on Oct 03, 2002 at 04:15 UTC | |
Re: The Effect of sort on Equal Items
by Zaxo (Archbishop) on Oct 03, 2002 at 04:39 UTC | |
by Dr. Mu (Hermit) on Oct 03, 2002 at 05:21 UTC |
Back to
Seekers of Perl Wisdom