Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108850 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 10279 invoked from network); 4 Mar 2020 20:16:02 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Mar 2020 20:16:02 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 941C31804E0 for ; Wed, 4 Mar 2020 10:35:27 -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.4 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_SOFTFAIL 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-f169.google.com (mail-qt1-f169.google.com [209.85.160.169]) (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 10:35:26 -0800 (PST) Received: by mail-qt1-f169.google.com with SMTP id v15so2159463qto.2 for ; Wed, 04 Mar 2020 10:35:26 -0800 (PST) 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=povKgwEwlPK5H/8sHm2wdlW9aRqPzl0SQjnvSh5P7/g=; b=hAsraax6INs1ayQUs45evwccOwqPZAVl+Fvqut6vUJHPi74mKqhy7qnrhROzV3dO+v vd21uZpt4/17RqZTo7Szrezk1NDxQ6DKc0edwgIeLQvUIKWbPf5NJzLY/TfGpVq8e2jN 5cgOh7hKG/2+KeuKEoXWRQpPmFBGBWmAAvV9nC78K/PZQHKuLHA+39MgZrJnJG37mHYN sai4hAPhNQRIVngnV0vsaHAJXFPgHSxFaTuyWCDUrGTYTytUtXdzLUzads7lZFvPYiG9 bSvquDPd2A7OBPBs2+PK9O1PQI+4uHRtqqzR2u1jpy7ZZkqnHdGR33Q6PZurcNlIkID7 0bSA== X-Gm-Message-State: ANhLgQ3Ka+C21vJKpP4Ydv3/ONd6Usb2YIt20xg05yWPJQdjbN/5Ohqa aJNM5B/YHdLxxHbugaDINlJ4v5pGy4SxQJEa4WT2AA== X-Google-Smtp-Source: ADFU+vtBxRzEyJhFv48Ohr18O3JiY81ot2hzWjDkeIaKbWqAype9MeqO+6BqgsAJ0z3B1vT5Nh/b5OLiAoPDFnvMQDo= X-Received: by 2002:ac8:c09:: with SMTP id k9mr2840177qti.241.1583346923881; Wed, 04 Mar 2020 10:35:23 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 4 Mar 2020 12:35:00 -0600 Message-ID: To: Nikita Popov Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000f4760b05a00baec4" Subject: Re: [PHP-DEV] Make sorting stable From: pollita@php.net (Sara Golemon) --000000000000f4760b05a00baec4 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 "stabl= e >> 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 a= nd > 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-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 fro= m > 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 letting people know they're doing it wrong seems win-win and low cost. -Sara --000000000000f4760b05a00baec4--