On Tue, Jun 21, 2005 at 08:58:02PM -0000, Randal L. Schwartz via RT wrote:
> # New Ticket Created by  (Randal L. Schwartz) 
> # Please include the string:  [perl #36354]
> # in the subject line of all future correspondence about this issue. 
> # <URL: https://rt.perl.org/rt3/Ticket/Display.html?id=36354 >
> 
> 
> 
> eq_set (I know, deprecated, but it's still broken) does this:
> 
>   sort { ref $a ? -1 : ref $b ? 1 : $a cmp $b } @$a1
> 
> I think the intent was to put all references at the beginning, and
> then compare everything else directly.  Maybe not.  Hard to tell.
> 
> However, if the entire list is references (as it is with Class::DBI's
> t/04-*.t test), then the sort routine gets "-1" for every pairwise
> comparison.  As this is undefined, the result is random, and apparently
> different on different machines, and different versions, and
> different compilers, as I've spent the last few hours diagnosing.
> 
> If the purpose is merely to provide a *consistent* sorting, this will
> suffice:
> 
>   sort { (ref $a) cmp (ref $b)   or    $a cmp $b } @$a1
> 
> This puts all the references after all the non-references, then sorts
> all the items according to their stringification.  I *think* that
> gets you 95% of the way there, if not 100%.
> 
> Anyway... classic example of how *not* to write a sort sub. :)


Yes. There's no reason to not be more explicite about ones intentions.
And you don't even have to use extra variables:

   sort (grep {ref} @$a1), sort (grep {!ref} @$a1)

It's probably faster as well, as it doesn't use a sort sub.


Abigail

Attachment: pgp5qWItyNQG0.pgp
Description: PGP signature

Reply via email to