Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108867 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 63826 invoked from network); 5 Mar 2020 13:46:05 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Mar 2020 13:46:05 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7EC341804D8 for ; Thu, 5 Mar 2020 04:05:45 -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,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-f179.google.com (mail-lj1-f179.google.com [209.85.208.179]) (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 04:05:44 -0800 (PST) Received: by mail-lj1-f179.google.com with SMTP id f13so5794005ljp.0 for ; Thu, 05 Mar 2020 04:05:44 -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=dSP98Zz4XiXtpbmQ9bUAGeMc+oM40w+k+2soh6lh5bY=; b=Hvg+92Qaeboe2ioWkY1kEEyeNtW4q5kFvsC1oRhg+QywXQlj1EGaW8WgSmdikRueX+ 4mN10EPaFKcnJKj2nqdXXC5QcWS+qrDUgK4NktkPd0ZNlLapt2Jx8fWbW6OKCVD1vR/I QzwEZzibKU98WnXEalQm0u0qTNKE8elVlc8kAl2oAdhf83iD1PP6NOJZpss96k7T38GT mi1o/yXHarAjC+sdxVg8mYUXhvr1sZo8yO3qNd9Fcb4IETYxGKuBCXBo0P8rWmfHwxmP JJQ8nxmX6FXp+8aRpEwDjZhCnISULF7NdBBv49QzBLw7nzn3OXI/j2Ge/tyme9cD4liJ 63uw== 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=dSP98Zz4XiXtpbmQ9bUAGeMc+oM40w+k+2soh6lh5bY=; b=DezC8pg3B1+mnNevMjsyWq+TLsKgggX+/Kl0pbui8uoKaEkL+7snJRgp9W0GusenDa yY6/1wgPCk1H207drZovemFQVV7dQmSgVCM9rjGW4mUzVTvzgptwNMD7/0+WG9Yvy3Qv CySG9z2312YXrsrMIJnsEWmBid6/OPvduHPomvClvgW7oLkISE07/BP5Itylpj98WzmK JF29MBqmPNz/6vLjCduAIeA46feJ1LNo91vNGy1uZSzkG0paR2oAaSioPEKXtFX2dHqJ /aX/0BNwLMNRes4VDGqYroh9SrrXgxzXWdYalYOOooqIZYvGP0gRCiiqcDT60Mesg2o0 aE3w== X-Gm-Message-State: ANhLgQ0/KnKXbUUeRfgTsMUD/DklbURrzhzTnop4scdu4cpAUgVlQ577 T1+WgQn/nzLrQ9AN5qtoNi3GhhvE9MkdLClLqMnjYQ3f X-Google-Smtp-Source: ADFU+vuRogjk3H/eScIcTaItR9My4NZmXLTDdTrM6r48zHLw9GMWt4/Tp52hNdIlEcHdPTecAUt9fGH+jZv+CnoywJM= X-Received: by 2002:a2e:86d0:: with SMTP id n16mr904663ljj.117.1583409943090; Thu, 05 Mar 2020 04:05:43 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 5 Mar 2020 13:05:26 +0100 Message-ID: To: "Christoph M. Becker" Cc: Sara Golemon , PHP internals Content-Type: multipart/alternative; boundary="00000000000031184b05a01a5b2d" Subject: Re: [PHP-DEV] Make sorting stable From: nikita.ppv@gmail.com (Nikita Popov) --00000000000031184b05a01a5b2d Content-Type: text/plain; charset="UTF-8" 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 what the cause is. > > > > 2. We can fix this transparently by doing something like this internally: > > > > $result = $compare($a, $b); > > if ($result !== false) { > > return (int) $result; // Normal behavior > > } > > > > // Bad comparison function, try the reverse order as well > > return -$compare($b, $a); > > > > That is, we will recover the full three-way comparison result from the > > "greater than" result, by checking for both $a > $b and $b > $a. > > 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. > > [1] > > This is a different case. The problem with my example is that it reports equality where none exists, which is an issue if you try to change the ordering of equal elements. Your case (which is indeed somewhat common in extension code) always returns "greater-than" for non-equal elements. The end result here is that the sort will not have any defined order regardless of how we perform it. We can't really do anything about such cases. I expect that a proposal for userland compare and equality overloading may want to make the "not ordered" case more explicit, in which case we could throw a warning when such comparisons are performed as part of sorting. Nikita --00000000000031184b05a01a5b2d--