Guest FirefoxRocks Posted September 12, 2009 Share Posted September 12, 2009 Suppose I have an object like this: SimpleXMLElement Object( [lastUpdate] => 2009-02-26 [team] => Array ( [0] => SimpleXMLElement Object ( [name] => Team A [points] => 13 ) [1] => SimpleXMLElement Object ( [name] => Team B [points] => 22 ) [2] => SimpleXMLElement Object ( [name] => Team C [points] => 9 ) )) I want to sort the teams by points, how do I do that? If I convert the objects to arrays, and even if I did, which function would I use to sort the array? Link to comment Share on other sites More sharing options...
Synook Posted September 13, 2009 Share Posted September 13, 2009 If you convert it to an array, you can use array_multisort(). Link to comment Share on other sites More sharing options...
Guest FirefoxRocks Posted September 13, 2009 Share Posted September 13, 2009 What is the argument that I need to use in array_multisort()? Currently I have implemented a JavaScript(jQuery) solution but this isn't the best way to solve the problem. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 14, 2009 Share Posted September 14, 2009 You probably want to use usort. You define a custom comparison function to compare the fields you want and say which goes first. Examples here:http://www.php.net/manual/en/function.usort.php Link to comment Share on other sites More sharing options...
Guest FirefoxRocks Posted September 15, 2009 Share Posted September 15, 2009 What should the custom function look like for me if I need to sort by POINTS and MEMBERS both in descending order? In the first example on the usort page, where does the $b variable come out of? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 15, 2009 Share Posted September 15, 2009 $a and $b are just two arbitrary elements in the list to sort, your function needs to decide which gets sorted first. So you would compare $a->points with $b->points, if they're both the same compare the members property. Return a positive value if $b should go before $a, a negative value if $a should go before $b, and 0 if they're equal. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.