Re: [PHP-DEV] Make sorting stable

2020-03-11 Thread Andrea Faulds
Hi Nikita! Nikita Popov wrote: 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

Re: [PHP-DEV] Make sorting stable

2020-03-11 Thread Mike Schinkel
> On Mar 4, 2020, at 11:42 AM, Nikita Popov wrote: > > Hi internals, > > Sorting function in PHP currently do not guarantee stability, which means > that the result order of elements that compare equal is not defined. > > > > What do people think about this? Is there interest in making so

Re: [PHP-DEV] Make sorting stable

2020-03-11 Thread Nikita Popov
On Wed, Mar 4, 2020 at 8:49 PM Larry Garfield wrote: > On Wed, Mar 4, 2020, at 12:35 PM, Sara Golemon wrote: > > On Wed, Mar 4, 2020 at 12:12 PM Nikita Popov > wrote: > > > > > On Wed, Mar 4, 2020 at 6:17 PM Sara Golemon wrote: > > > > > >> On Wed, Mar 4, 2020 at 10:42 AM Nikita Popov > > >> w

Re: [PHP-DEV] Make sorting stable

2020-03-11 Thread Nikita Popov
On Fri, Mar 6, 2020 at 12:58 AM Andrea Faulds 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

Re: [PHP-DEV] Make sorting stable

2020-03-10 Thread Stanislav Malyshev
Hi! > Given that we have internal classes which deliberately have such > comparison behavior (i.e. returning 0 or 1, to signal that there is no > order defined), e.g. Closures[1], I tend to prefer raising a warning > instead of trying to recover. I think there weirdness is because equality and co

Re: [PHP-DEV] Make sorting stable

2020-03-05 Thread Andrea Faulds
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 ope

Re: [PHP-DEV] Make sorting stable

2020-03-05 Thread Christoph M. Becker
On 05.03.2020 at 13:05, Nikita Popov wrote: >> Given that we have internal classes which deliberately have such >> comparison behavior (i.e. returning 0 or 1, to signal that there is no >> order defined), e.g. Closures[1], I tend to prefer raising a warning >> instead of trying to recover. >> >> [

Re: [PHP-DEV] Make sorting stable

2020-03-05 Thread Nikita Popov
On Wed, Mar 4, 2020 at 11:12 PM Christoph M. Becker wrote: > On 04.03.2020 at 19:11, Nikita Popov wrote: > > > 1. As Tyson suggests, we can throw a warning if a boolean is returned > from > > the comparison callback (probably with a check to only throw it once per > > sort), to make it obvious wh

Re: [PHP-DEV] Make sorting stable

2020-03-05 Thread Nikita Popov
On Wed, Mar 4, 2020 at 7:35 PM Sara Golemon wrote: > On Wed, Mar 4, 2020 at 12:12 PM Nikita Popov wrote: > >> On Wed, Mar 4, 2020 at 6:17 PM Sara Golemon wrote: >> >>> On Wed, Mar 4, 2020 at 10:42 AM Nikita Popov >>> wrote: >>> The only issue I ran into is that this change has a negative

Re: [PHP-DEV] Make sorting stable

2020-03-05 Thread Nikita Popov
t too large, it is better to have a single behavior, rather than adding more flags to our (many) sort functions. Regards, Nikita > -- > *From:* Nikita Popov > *Sent:* Wednesday, March 4, 2020 19:42 > *To:* PHP internals > *Subject:* [PHP-DEV] Make sor

Re: [PHP-DEV] Make sorting stable

2020-03-04 Thread Andrea Faulds
Christoph M. Becker wrote: Given that we have internal classes which deliberately have such comparison behavior (i.e. returning 0 or 1, to signal that there is no order defined), e.g. Closures[1], I tend to prefer raising a warning instead of trying to recover. Oof… I wonder if we should make F

Re: [PHP-DEV] Make sorting stable

2020-03-04 Thread Christoph M. Becker
On 04.03.2020 at 19:11, Nikita Popov wrote: > 1. As Tyson suggests, we can throw a warning if a boolean is returned from > the comparison callback (probably with a check to only throw it once per > sort), to make it obvious what the cause is. > > 2. We can fix this transparently by doing something

Re: [PHP-DEV] Make sorting stable

2020-03-04 Thread Andrea Faulds
Hi, Larry Garfield wrote: If I'm understanding the definition of stable here, it means that if two values evaluate to equal they will always end up in the same order in the output that they were in the input, yes? So (trivial example): $a = ["3", 2, 3, 5]; Sorts to: [2, "3", 3, 5]; always

Re: [PHP-DEV] Make sorting stable

2020-03-04 Thread Andreas Hennings
Note that a comparison callback can also be broken in other ways: - It could contain a loop, e.g. 'a' < 'b', 'b' < 'c', 'c' < 'a'. - It could have alternating return values in subsequent calls with the same arguments. This kind of misbehavior cannot be easily detected. The best we can do is make s

Re: [PHP-DEV] Make sorting stable

2020-03-04 Thread Larry Garfield
On Wed, Mar 4, 2020, at 12:35 PM, Sara Golemon wrote: > On Wed, Mar 4, 2020 at 12:12 PM Nikita Popov wrote: > > > On Wed, Mar 4, 2020 at 6:17 PM Sara Golemon wrote: > > > >> On Wed, Mar 4, 2020 at 10:42 AM Nikita Popov > >> wrote: > >> > >>> The only issue I ran into is that this change has a n

Re: [PHP-DEV] Make sorting stable

2020-03-04 Thread Sara Golemon
On Wed, Mar 4, 2020 at 12:12 PM Nikita Popov wrote: > On Wed, Mar 4, 2020 at 6:17 PM Sara Golemon wrote: > >> On Wed, Mar 4, 2020 at 10:42 AM Nikita Popov >> wrote: >> >>> The only issue I ran into is that this change has a negative impact on >>> code >>> using illegal comparison callbacks like

Re: [PHP-DEV] Make sorting stable

2020-03-04 Thread Nikita Popov
On Wed, Mar 4, 2020 at 6:17 PM Sara Golemon wrote: > On Wed, Mar 4, 2020 at 10:42 AM Nikita Popov wrote: > >> The only issue I ran into is that this change has a negative impact on >> code >> using illegal comparison callbacks like this: >> >> usort($array, function($a, $b) { >> return $a >

Re: [PHP-DEV] Make sorting stable

2020-03-04 Thread Nicolas Grekas
Le mer. 4 mars 2020 à 18:30, tyson andre a écrit : > > What do people think about this? Is there interest in making sorting > > stable? Is it okay to break code using illegal comparison callbacks? > > I'd be interested in having a stable sort. > A new SORT_STABLE flag would be great!

Re: [PHP-DEV] Make sorting stable

2020-03-04 Thread tyson andre
> What do people think about this? Is there interest in making sorting > stable? Is it okay to break code using illegal comparison callbacks? I'd be interested in having a stable sort. When migrating from php 5.6 to 7 a long time ago, the fact that sorting was no longer stable was an inconvenience

Re: [PHP-DEV] Make sorting stable

2020-03-04 Thread Sara Golemon
On Wed, Mar 4, 2020 at 10:42 AM Nikita Popov wrote: > The only issue I ran into is that this change has a negative impact on code > using illegal comparison callbacks like this: > > usort($array, function($a, $b) { > return $a > $b; > }); > > Let's define what "negative impact" means in this

[PHP-DEV] Make sorting stable

2020-03-04 Thread Nikita Popov
Hi internals, Sorting function in PHP currently do not guarantee stability, which means that the result order of elements that compare equal is not defined. To achieve a stable sort, you need to do something like this (untested): $arrayAndPos = []; $pos = 0; foreach ($array as $value) { $arr