Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108851 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 19089 invoked from network); 4 Mar 2020 21:29:21 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Mar 2020 21:29:21 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7EA001804D9 for ; Wed, 4 Mar 2020 11:48:50 -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=-2.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS11403 64.147.123.0/24 X-Spam-Virus: No X-Envelope-From: Received: from wout4-smtp.messagingengine.com (wout4-smtp.messagingengine.com [64.147.123.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 4 Mar 2020 11:48:49 -0800 (PST) Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.west.internal (Postfix) with ESMTP id 83DEB513 for ; Wed, 4 Mar 2020 14:48:48 -0500 (EST) Received: from imap26 ([10.202.2.76]) by compute7.internal (MEProxy); Wed, 04 Mar 2020 14:48:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=6BLcUcoM7dxbZ85jgcDl9NE4GscJ6KZP3KpW4i3D1 6w=; b=2GB5u9Z0PuSkLAZFyAd8sqc60o2FZ+zXVAQjljKlhteLGQzI1LNN/l8Jn CV7cM90pcGzXJhTTBYRuA4bsFI8iV+OdEpO9ZOZgPb/CD48cuaC4CcvzkQ+AiFit 2hLSoq6Ntni0jisEpscUxz24E1v2SjuxIOMqZraO2MCcGQRCMxhsOAW47E+YsirC h9ftq9D0blACBFP7uxGcOpdYzuNre6iuTrDp0P7R3+C0OHrpgPqMyjinFmI9kLQp KmiiJoaBvKM+z6bH31pWhnxRzuEDt1l44SbV6vd/0QMDFaSbq1tpPubgbg46ld0e Y5EQGU7UULDRfvidnnUYgAkpFmyqw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedruddtkedgudeftdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvufgtgfesthhqredtreerjeenucfhrhhomhepfdfn rghrrhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrd gtohhmqeenucffohhmrghinhepshhtrggtkhhovhgvrhhflhhofidrtghomhenucevlhhu shhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrg hrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id ABED414200A2; Wed, 4 Mar 2020 14:48:47 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.1.7-986-gfc2d493-fmstable-20200304v3 Mime-Version: 1.0 Message-ID: <9a590afe-8674-4d5c-b18f-2bc15786348c@www.fastmail.com> In-Reply-To: References: Date: Wed, 04 Mar 2020 13:48:21 -0600 To: "php internals" Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Make sorting stable From: larry@garfieldtech.com ("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 wr= ote: >=20 > > 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 impac= t 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 t= hat > >> one still winds up with an essentially sorted array, but hitherto "= stable > >> appering" output is now stable in a different way? Or is the resul= t > >> actually just NOT sorted in a way that a reasonable user would cons= ider > >> correct (e.g. 5 sorted before "3")? > >> > > > > "Negative impact" is PR speak for "completely broken" ;) Yes, it cou= ld > > sort 5 before 3. The comparison function becomes completely ill-defi= ned and > > you get Garbage-In-Garbage-Out. > > > > Roger that, thanks for explaining. =F0=9F=91=8D >=20 >=20 > > If we are concerned about this (I'm not sure we should be, but I've = at > > least seen people be interested about this peculiar behavior: > > https://stackoverflow.com/questions/59908195/how-come-usort-php-work= s-even-when-not-returning-integers), > > there's two things we can do: > > > > 1. As Tyson suggests, we can throw a warning if a boolean is returne= d 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 intern= ally: > > > > $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? >=20 > Fixing broken stuff transparently for a smooth upgrade path PLUS letti= ng > people know they're doing it wrong seems win-win and low cost. >=20 > -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 Doi= ng 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 t= he 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], some= what randomly. Am I understanding correctly? --Larry Garfield