Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:40881 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 14231 invoked from network); 7 Oct 2008 22:50:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Oct 2008 22:50:35 -0000 X-Host-Fingerprint: 64.58.161.125 wsip-64-58-161-125.oc.oc.cox.net Received: from [64.58.161.125] ([64.58.161.125:7851] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2E/42-48736-AB7EBE84 for ; Tue, 07 Oct 2008 18:50:35 -0400 Message-ID: <2E.42.48736.AB7EBE84@pb1.pair.com> To: internals@lists.php.net Date: Tue, 07 Oct 2008 15:50:23 -0700 User-Agent: Thunderbird 2.0.0.17 (Windows/20080914) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 64.58.161.125 Subject: RFC: SPL Comparator interface From: jeremy@pinacol.com (Jeremy) Something that would be extremely useful is a comparator interface in SPL, which could be used to easily implement sorting for objects. Example: class Foo implements Comparator { // is numeric for this example public $bar; //implements compare method from Comparator interface public function compare($that) { //simple example; will return < 1 if this is less //than that; zero if they're equal; > 1 if this is //greater than that return $this->bar - $that->bar; } } Then, if I had an array of Foo objects, I could just call some kind of spl_sort function on the array which would use my comparator to sort them. I realize this can be kludged with usort(), but I think there should be a better, OOP-ier way to do it. I also realize that a similar effect could be achieved by defining an SPL PriorityQueue-ish collection rather than an array, but I think there ought to be a better way. Java has a Comparator interface which works in much the same way, if that helps my case at all. Jeremy