Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108865 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 60366 invoked from network); 5 Mar 2020 13:39:10 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Mar 2020 13:39:10 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A56501804F3 for ; Thu, 5 Mar 2020 03:58:48 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS 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-lj1-f195.google.com (mail-lj1-f195.google.com [209.85.208.195]) (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 ; Thu, 5 Mar 2020 03:58:48 -0800 (PST) Received: by mail-lj1-f195.google.com with SMTP id h18so5724379ljl.13 for ; Thu, 05 Mar 2020 03:58:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=WPknDVkkktLBI8GF0nsBIOyCHh+Nkg4ZegugWDvSj/g=; b=Wq0ZjyFaUt4MB6wmioTDeDHlvm3P77D5MzSWD1oVpPnniFqnSlmoMiOw7Gi//UVS+k 6mnu4/Z/hsiIyNOfeaUeFJdl5wCby20V5+2ahapkp6+a2OM6YZsVwHr6fucjVGW4fHqn 5NXogyVMiaBpZVTlo4p/5iNAGyhI93wVzcz0/ndBU5dNrfjGNxXTbohHrAiKDeCySaeC qv7zvRM4lrmWKUYMqKGimCC3n7dWEj+NKE3IO/VhRXFtZ8+xF05nYFwQdHgykJkXxSRS iforeZPEduPhmGOFgXhV6kLWpLB4C7wmyH535jLtnCWp5VSxTsvFwNOdXTAoGyrOnPNF fN0g== 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=WPknDVkkktLBI8GF0nsBIOyCHh+Nkg4ZegugWDvSj/g=; b=eXNEfolw1dV3kweizVeBsc6LhFw8GiyPUXkQHmk2ZtAKM7eQC6tvz7SO1I+acdNy1F Txk7zgwV44lDGBOoJTsRXZryoOCkVor+V6vRhKbFmOhOtHCWy1dR+da7U7HTVhsjhh34 +2Rjz/syFg8C/T3Tqwz1vU6zvitA7BzTBqYMzghoL5YJrti9sVIC40rBYVUh/j/tUoKm NFEJC6O7yMGiqV8O6ScQ3XCPxfLk43AaHLcuORBzt3LVwuUGhXXaHF7vYZdlJkGy18ao byWPUm4zSO4UwZvTNLZ9alA7henSPzRJB5r9OpP6Pf/rO9flJgvJd6Findp/4zzJFE97 BHVw== X-Gm-Message-State: ANhLgQ0ssFxfss+raYgtZ81mlUWvpwSF6fM/U6h4kXTLIh4LaDbhNemC idX5ZbM4BnYcRZZny+HXJv+lF2+StRryZTpyLIk= X-Google-Smtp-Source: ADFU+vu8sNNTvKjFKivDHlkpNuOC9tvii8EyHWynGEwo2dAhc7H46qMDFKZExrXH8dl7ATpPwP0KcbW8VoJwgap9/8Q= X-Received: by 2002:a2e:7806:: with SMTP id t6mr5216067ljc.145.1583409526552; Thu, 05 Mar 2020 03:58:46 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 5 Mar 2020 12:58:30 +0100 Message-ID: To: Sara Golemon Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000005d3beb05a01a4204" Subject: Re: [PHP-DEV] Make sorting stable From: nikita.ppv@gmail.com (Nikita Popov) --0000000000005d3beb05a01a4204 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 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 that >>> one still winds up with an essentially sorted array, but hitherto "stab= le >>> 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 could >> 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 at >> least seen people be interested about this peculiar behavior: >> https://stackoverflow.com/questions/59908195/how-come-usort-php-works-ev= en-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 onc= e >> 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 letting > people know they're doing it wrong seems win-win and low cost. > 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 --0000000000005d3beb05a01a4204--