Re: usort for sorting an array of objects

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Dec 10, 2008 at 10:27 PM, Stut <stuttle@xxxxxxxxx> wrote:

> On 10 Dec 2008, at 04:15, German Geek wrote:
>
>> I need to sort an array of objects. I found this ( at a url that didnt let
>> me send this msg... ) and I would know how to do it, but I believe there
>> might be a cleaner, more elegant way to do it. In Java, you just need to
>> implement the interface Comparable and provide a method called compareTo
>> (as
>> far as i remember) and then you can use one of the many sorting algorithms
>> generically on objects that are comparable...
>>
>> Anyway, I didn't find something like that for PHP. Since I'm using
>> symfony,
>> I had a bit of a play with the objects at hand and simply did a
>> sort($arrayOfObjects) and it did sort them by the id. Just wondering where
>> it got the information on what to sort on (not quite) correctly for my
>> case?
>>
>
> I'm confused. The function you need is the one you mention in the subject.
> All you need to do is create a function that compares two of the objects, in
> whatever way you need it to, and returns -1, 0 or 1. The pass that to the
> usort function - it doesn't care what type of thing is in the array. Full
> details available at http://php.net/usort.



I just ment to say it would be nice to have it like in Java or C# where you
can implement an interface, called Comparable and define a function in the
class called compareTo which returns an integer smaller, equal or greater
than 0 by which elements can generically be sorted by in a collection. Since
PHP has arrays (hash maps) as the primary collection type, and they are very
good for storing sets or other collections, it would be nice to have a sort
function that would look, if the elements of the array have a compareTo
method (implement the Comparable interface), and if they do, then sort with
that. I find it a bit surprising that such a well designed programming
language doesn't have such a useful feature. I guess one could use one of
the gazillion libraries out there to do the same thing. Also, one could
argue that this further checking would slow down functions that are
primarily used for sorting strings. However, the answer could be also in the
ArrayObject class which is in php natively. Only it should implement all the
array functions that are there anyway, which shouldnt be too hard to do for
the PHP PL developers. Some more in depth documentation of that class would
also be helpful.

Anyway, I found a not perfect, but good enough solution:

Implement a static compare function in the class of the object and put a
function in my library, that is simply called myTools::sort that will get
the object class of the first element of the array (if there is one) and
sort according to the compare method implemented in that class (if it
exists), otherwise just use sort. The compare method takes the parameters as
self:

public class AClass { // implements Comparable {
  public static function compare(self $obj1, self $obj2) {
    return strcmp($obj1->prop1, $obj2->prop1); // e.g.
  }
}

That way I will get an exception if there is an object in the array which
does not have that class, but i can live with that. At least i'll get an
exception and not another funny error, because the parameters are self,
right?

It might be better to define an interface called Comparable with that
method, but was not really necessary for my case.

Just a few thoughts to maybe improve PHP in the future. Hopefully there will
be a lot of interfaces and objects for collection types at some stage in PHP
natively, although that might clutter the namespace and could be realised
with libraries. What are your thoughts?

-- 
Tim-Hinnerk Heuer

http://www.ihostnz.com

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux