I'd like to sort an array of numbers (while maintaining the indexes) in a descending order.
The thing is that if there are duplicate values, I'd like them to be sorted by the matching values in a second array.
For instance, if the array is:
0 => 13,
1 => 21,
2 => 15,
3 => 21
And the second array is:
0 => 3,
1 => 2,
2 => 7,
3 => 4
Then I'd like 3 => 21
to come before 1 => 21
, because the matching value of index 3
in the second array is bigger than index 1
(4 > 2
).
So the final sorted array would be:
3 => 21,
1 => 21,
2 => 15,
0 => 13
The actual purpose of this is as follows:
Figure a poll. Every user has to rank the options from 1 to X (X = number of options).
If a user ranks the an answer as the first rank, it gets X points, the second one gets X-1, and so on.
So the first array is actually the score of each option (the index is the option's id).
The second array is the number of users who ranked the option first (the index is the option's id, matching to the indexes in the first array).
So when there are two or more options with the same score, the one that has more first rank votes is ranked higher.
Hope that makes sense.
Thanks!