Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A unique sorting problem

Status
Not open for further replies.

dpm8

Programmer
Dec 14, 2004
8
0
0
US
Howdy folks.

I've got a sorting problem. I have a bunch of data in this format.

[a number] [a mixed array]
so...
[312] ["cow", 2, 3, 4, 6]
[345] ["duck", 2, 5, 4, 6]
[312] ["gnat", 7, 3, 6, 3]
[318] ["cow", 2, 3, 4, 9]

so the first element is a rating. the second element is the array that the rating pertains to. I'm trying to sort this huge list by the rating. The problem is, i can't throw them into a 2d array because some of the ratings are the same and you can't have identical keys. And i tried sorting by the value, but the value is an array itself. I'd like it to sort like this.

[345] ["duck", 2, 5, 4, 6]
[318] ["cow", 2, 3, 4, 9]
[312] ["cow", 2, 3, 4, 6]
[312] ["gnat", 7, 3, 6, 3]

where they're sorted by rating in reverse order much like krsort. if the rating IS the same, it doesn't matter what order they are in as long as they are grouped.

ANy suggestions?

Thanks,
Danny
 
does this work for you?

Code:
<?
//establish the array
$array = array(
array(312, array("cow", 2, 3, 4, 6)),
array(345, array("duck", 2, 5, 4, 6)),
array(312, array("gnat", 7, 3, 6, 3)),
array(318, array("cow", 2, 3, 4, 9))
);

sort($array);


//sorted now in order
//need to reverse it
$rev = array_reverse($array);

foreach ($rev as $key=>$val)
{
	echo "$key ||".print_r($val);
	echo "<br />";
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top