Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108853 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 24090 invoked from network); 4 Mar 2020 21:53:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Mar 2020 21:53:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BF8721804DA for ; Wed, 4 Mar 2020 12:13:21 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-qt1-f174.google.com (mail-qt1-f174.google.com [209.85.160.174]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 4 Mar 2020 12:13:20 -0800 (PST) Received: by mail-qt1-f174.google.com with SMTP id h16so2377302qtr.11 for ; Wed, 04 Mar 2020 12:13:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=00QI5tyBV6446KRiwvUhTRY8ztssLJsODpAOxJLX6HQ=; b=LEhfnlrLQFM4CqcUN1wrP24zMQ+HpWS7y2GjPCn9VkCswuyoDrSXOzOtI/wClbQ69+ NPnwtzM3oKoYZpQyQuS9k0KyuJ7ycEaZBKE4woU7dSZs/6h9WFXA8mikXjGmD2BTs56e YwMOVF7IMgb2anutKvdTLTF6eC5bjSC5wTkAPcAVnZEovZ0b9pw+RT9TImVtVb0BfFdO e8MDR5TY4GNHmyPvJkQXFCfL4uotmQH7rVt0AxdXxKmoSZGyIkZOG72T4AEj3H8rViep 2jZWaUyaKO5LoKTfM7pBVXYI9GiGPvbsZc5q4dcXdz5SH4y1PGfRv7VqOf4p9bhg7PkC CXtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=00QI5tyBV6446KRiwvUhTRY8ztssLJsODpAOxJLX6HQ=; b=SW+Z5rRThoZ+tuBEJiMxoPVoJrNt2pTCfBO4KcUvMAUUcT8ol9EWpY7DscgAxrpbQF 6pymva82Un0wfCnc6wlxYdFkN9JzsLOQjrm7m32T8wyvzNS4bmRjW+JQOqdubm2UmFTs +x+j3/+dacOKuewD3C7RikyFYPdIbs7HD+VIUJwrez3Lcn5EROiRsQDcO4xpyYOHRd0h GdEKVTCLkvMoQCryeP9jx5PxBehE89MME5WrcVO274DfGVhKh0coKWsYueMzC+qjGFd6 xi12kpOQJ2NdGsYR3OZlgYBTJToBXLlUTLd6LfQmY6Fezeu36Xl8Yhu5Av1LIKt8UoxQ 7OJg== X-Gm-Message-State: ANhLgQ0peUcf45y12AUJozb9hkLCQwP1qC5Dmr60GI4xn0liVTIRGIN6 0orXJKskE/ngAyv3jd7GmWkHrAP7xJk= X-Google-Smtp-Source: ADFU+vvPd65ypODBXtu2/K1IsvdsQMF3ufFg+Sgo2ETaHIXEx4eMNEucC6MLpU77NkDT4v6vCWC+jQ== X-Received: by 2002:ac8:669a:: with SMTP id d26mr4090242qtp.304.1583352799670; Wed, 04 Mar 2020 12:13:19 -0800 (PST) Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com. [209.85.222.171]) by smtp.googlemail.com with ESMTPSA id h25sm4588354qtn.30.2020.03.04.12.13.18 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 Mar 2020 12:13:18 -0800 (PST) Received: by mail-qk1-f171.google.com with SMTP id u124so2959010qkh.13 for ; Wed, 04 Mar 2020 12:13:18 -0800 (PST) X-Received: by 2002:a37:9f41:: with SMTP id i62mr2951226qke.494.1583352798326; Wed, 04 Mar 2020 12:13:18 -0800 (PST) MIME-Version: 1.0 References: <9a590afe-8674-4d5c-b18f-2bc15786348c@www.fastmail.com> In-Reply-To: <9a590afe-8674-4d5c-b18f-2bc15786348c@www.fastmail.com> Date: Wed, 4 Mar 2020 21:13:07 +0100 X-Gmail-Original-Message-ID: Message-ID: To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="00000000000019330605a00d0db7" Subject: Re: [PHP-DEV] Make sorting stable From: andreas@dqxtech.net (Andreas Hennings) --00000000000019330605a00d0db7 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 sure these kinds of comparison functions do not cause infinite loops. This said: Yes, stable sort out of the box would be a welcome feature. On Wed, 4 Mar 2020 at 20:49, 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 > > >> 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 regard. Is it th= at > > >> one still winds up with an essentially sorted array, but hitherto > "stable > > >> appering" output is now stable in a different way? Or is the result > > >> actually just NOT sorted in a way that a reasonable user would > consider > > >> correct (e.g. 5 sorted before "3")? > > >> > > > > > > "Negative impact" is PR speak for "completely broken" ;) Yes, it coul= d > > > sort 5 before 3. The comparison function becomes completely > ill-defined and > > > you get Garbage-In-Garbage-Out. > > > > > > Roger that, thanks for explaining. =F0=9F=91=8D > > > > > > > If we are concerned about this (I'm not sure we should be, but I've a= t > > > least seen people be interested about this peculiar behavior: > > > > https://stackoverflow.com/questions/59908195/how-come-usort-php-works-eve= n-when-not-returning-integers > ), > > > there's two things we can do: > > > > > > 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 like this > internally: > > > > > > $result =3D $compare($a, $b); > > > if ($result !=3D=3D false) { > > > return (int) $result; // Normal behavior > > > } > > > > > > // Bad comparison function, try the reverse order as well > > > return -$compare($b, $a); > > > > > > > > =C2=BFPor que no los dos? > > > > Fixing broken stuff transparently for a smooth upgrade path PLUS lettin= g > > people know they're doing it wrong seems win-win and low cost. > > > > -Sara > > > I concur. If a comparison function returns a non-legal value: > > 1) Fold it to a legal value. if feasible. > 2) Raise a notice/warning, maybe deprecated? that tells them they're Doin= g > It Wrong(tm) > 3) Sometime in the future turn that notice/warning into a hard error. > > 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 =3D ["3", 2, 3, 5]; > > Sorts to: > > [2, "3", 3, 5]; > > always, whereas right now it may sort to that or to [2, 3, "3", 5], > somewhat randomly. Am I understanding correctly? > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --00000000000019330605a00d0db7--