* Brendan Eich wrote: >We could also introduce binary <=>, AKA "cmp", return -1, 0, or 1. >Imagine the sort fun: > > a.sort((a, b) => a <=> b) > >:-P. The win over using > > a.sort((a, b) => a - b) > >is that <=> would work as expected for string-typed a and b as well.
In Perl <=> compares as if the operands had been converted to numbers and the cmp operator as if the operands had been converted to strings. If you are suggesting to have `<=>` change behavior based on type in- spection at runtime, that behavior might lead to subtle bugs when the array contains strings and numbers (e.g., you might expect swapping the operands is the same as reversing the result, but it might not be. Indeed, without specifying the exact sorting algorithm you might get different sort orders in different implementations due to differences in which values are compared in which order). Having two operators a- voids that. The other day I published http://search.cpan.org/dist/List-OrderBy/ which takes a lesson from .NET and offers better syntax for multi-key sorts, `my @sorted = order_by { ... } then_by { ... } @unsorted;`. I ended up with a not-so-nice list of variants to support ascending and descending order and numeric and string comparisons. Thinking about that now makes me wonder if the Voyager holodeck has and requires an Arch console for advanced uses. In Haskell you would never write code like `a.sort((a, b) => a <=> b)` as, to paraphrase, `a.sort(<=>)` is much more clear. -- Björn Höhrmann · mailto:[email protected] · http://bjoern.hoehrmann.de Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de 25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/ _______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

