Re: Make sorting stable
| From: | Nikita Popov | Date: | Wed, 11 Mar 2020 10:25:49 +0000 |
| Subject: | Re: Make sorting stable | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108974@lists.php.net to get a copy of this message | ||
On Fri, Mar 6, 2020 at 12:58 AM Andrea Faulds <ajf@ajf.me> wrote:
> Hi,
>
> Nikita Popov wrote:
> >
> > I've implemented this variant now. If the comparison function returns a
> > boolean, you get
> >
> >> Deprecated: usort(): Returning bool from comparison function is
> > deprecated, return one of -1, 0 or 1 instead in %s on line %d
> >
> > once per usort() call, and we retry with swapped operands to ensure
> > compatibility.
> >
> > Regards,
> > Nikita
> >
>
> I think that message could be a little misleading, because any negative
> or positive value is valid, not just those of magnitude 1, right? IIRC
> strcmp($a, $b) doesn't just return -1 and 1, and of course this is also
> the case for the classic ($a - $b).
>
I've update the message to say:
> Deprecated: usort(): Returning bool from comparison function is
deprecated, return an integer less than, equal to, or larger than zero in
%s on line %d
The astute reader will notice that this is equivalent to "any integer", but
I think phrased this way, the meaning should be clear to anyone familiar
with the basic concept of comparison functions, while not mentioning -1, 0
and 1 in particular.
Regards,
Nikita
PS: Yes, I'm aware that you can also return a floating point number. No,
you should definitely not do that, because it's going to get casted to an
integer and produce wrong results if the numbers get large enough.