php - Secondary ranking by elimination within in an array of arrays
问题描述
I'm writing a golf ranking leaderboard based on the json example data below. I can easily rank the order from lowes (best) score to highest (worse) however when it comes to "ties" this is the issue I'm trying to figure out.
Here's a quick background: At array[0] is the final score, array[1] is the team number (T1,T2, etc) and array[2]-[10] is the sequential shots made per hole. Add up the sequential shots and subtract 36 which will equal array[0] for each team.
The tiebreakers now should be "re-ranked" according to the lowest score of each hole in sequence. For instance when comparing T11, T2 and T5... T2 should be ranked overall second because they shot 3 on the first sequential hole while T11 and T5 shot 4. Now that T2 is "re-ranked" as overall 2nd and out of the comparison, T11 would now compare with T5 for the next "re-rank" and so on for all tied teams. If anyone could give me a scenario I'm not looking for a script just basically need to temp extract the tied teams as segmented by the tied score i.e. extract T11, T2 and T5 (-5 score) for comparison and extract T10 and T9 (-4 score) as well as T!, T3, T4, and T7 (-3 score) for re-ranking then put those "re-ranked" segments back into the original array without disrupting the the teams that have not been compared in their original rank, i.e. T8 (-7 score) and T6 (-1 score).
{
[-7,"T8","4","3","3","4","4","2","3","3","3"],
[-5,"T11","4","3","4","4","4","3","3","3","3"],
[-5,"T2","3","3","4","4","4","3","3","4","3"],
[-5,"T5","4","4","3","4","4","3","4","3","2"],
[-4,"T10","4","3","4","4","4","2","4","4","3"],
[-4,"T9","4","3","3","4","5","3","3","3","4"],
[-3,"T1","5","3","3","4","4","3","4","4","3"],
[-3,"T3","4","3","4","4","4","3","4","4","3"],
[-3,"T4","4","3","3","4","5","3","4","3","4"],
[-3,"T7","4","4","4","4","3","3","4","4","3"],
[-1,"T6","4","3","3","4","5","4","4","4","4"]
}
Thank you.
解决方案
一种简单的方法是将数组的连续镜头部分的比较添加到排序比较函数中。
usort($data, function($a, $b) {
return ($a[0] <=> $b[0]) ?: (array_slice($a, 2) <=> array_slice($b, 2));
});
第一部分,$a[0] <=> $b[0]
只是比较第一个元素,我认为这类似于你已经在做的事情。
第二部分,array_slice($a, 2) <=> array_slice($b, 2)
只有当第一部分由于运算符而相等时才会被评估?:
。
数组切片可以直接比较,因为如果我理解正确的话,PHP 中比较数组的方式恰好就像您想要比较连续镜头的方式一样。只要数组的大小相同,它们一次只比较一个元素,从左到右(这在PHP 比较运算符文档中的“与各种类型的比较”表中进行了描述)。
如果可能的话,我认为如果您可以按团队编号索引数据会更方便,例如
[
"T8" => [-7,"4","3","3","4","4","2","3","3","3"],
"T11" => [-5,"4","3","4","4","4","3","3","3","3"],
...
]
那么你所需要的排名就是
asort($data);
推荐阅读
- java - Spring Security SAML 扩展,具有跨多个 tomcat webapps 的记住我功能
- javascript - MDN“实验”和 TC39 阶段之间是否有映射?
- ruby-on-rails - 尝试使用 ruby 发送批量电子邮件时出错
- api - 在颤动中从服务器获取图像到gridview
- rxjs - 如何根据第一个响应在 RxJS 中发出多个 Ajax 请求
- python - 从桌面路径显示图像
- python - 如何在 NetworkX 中读取语义图?
- mysql - 需要重命名一列中的数据,MySQL
- c++ - 如何测试注册表项是否存在?
- jmeter - Jmeter不发送名为Cookie的标头