I’m ranking users by a numerical criteria using asort(), then displaying them on a page. The array key is the user’s ID and the value is their rank. This is working just fine, however, what if I run into multiple users with the same rank - who have tied each other? I know this should be relatively easy, but I can’t get my brain unstuck to see the answer.
asort($user_rankings, SORT_NUMERIC);
// assign a numerical value (rank order) starting at 1
$i = 1;
foreach($user_rankings as $key=>$value) {
$rankings[$key] .= $i;
$i++;
}
Here’s the $user_rankings array after print_r($user_rankings);
OK, let’s say I have 4 users, and I want to rank them by weight.
Tom = 150 lbs
Ann = 110 lbs
Art = 170 lbs
Ted = 150 lbs
As you can see, 2 users have the same weight, so, in essence, they’re tied for the same position on the list. Just using a sorting function it would look like this (sorting from lightest to heavest)
Ann
Tom
Ted
Art
But, since Tom and Ted weigh the same, they are both in the #2 position. So the list should really look like this, which is what I’m having difficulty acheiving.
Aaaaaaaad, the plot thickens. Each person belongs to a team and they need to be sorted compared to their other team members. Currently they’re being sorted globally. So, if Bob, Tom and Arnold belong to team 1 I would only rank them against each other. And if Ann and Ted belonged to team 2, I would NOT want to rank them against Bob, Tom and Arnold.
I changed part of ScallioXTX’s script to the following, but I’m not sure this is the right way to go…
Sorry about the syntax below… the forum wouldn’t format it correctly using php
I racking my rain at this. I’m soooo close, but it’s just not working. This is a simplified version of the array I’m dealing with. I’m having a hard time sorting the users by team and then ranking them. I think at this point I’m just too close and just can’t see past what I’ve already done.
I could figure this out by performing a sql query for each team, but I’d like to try and use the information I already have, which is currently driving me nuts
Going through my testing I found that all users with a weight of 0 (or a negative weight) would tie for first place, so I altered the function, specifically the foreach where it increments the count. It seems to work OK. See any problems with it?
@AnthonySterling; Your custom sorter will only ever return 0 or 1 (okay, 0, true or false, coerced this is 0 or 1) so I’m not sure if this will even work.
As a general hint, people tend to think you need to return -1, 0, or 1, but in fact the magnitude doesn’t matter; PHP only cares about the direction (thanks again @Salathe; for that hint!). So the simplest thing to do when writing a custom sorter is to simply return $a - $b; – or indeed the indeed the other way around, depending on whether you want ascending or descending sorting.
Sound advice, but only useful for integers. Doing the same with floats will give unexpected results where the difference is between -1 and 1, exclusive, since PHP converts the return value of the comparison function to an integer.