Jump to content

Sorting Objects By ...members?


Guest FirefoxRocks

Recommended Posts

Guest FirefoxRocks

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

Guest FirefoxRocks

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

Guest FirefoxRocks

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

$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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...