Re: usort for sorting an array of objects

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

 



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.

-Stut

--
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[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