根据值获取列表中项目的索引

编程入门 行业动态 更新时间:2024-10-23 13:23:03
本文介绍了根据值获取列表中项目的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

该方案适用于足球联赛桌.我可以按比赛获胜率排序,然后按进球数来确定名单在联盟中的位置.然后,我使用此顺序使用IndexOf函数获得球队在联赛表中的排名.

The scenario is for a football league table. I can order the list by match win percentage and then by goals scored to determine their position in the league. I then use this ordering to get teams position in the league table using the IndexOf function.

this.results = this.results.OrderByDescending(x => x.WinPercentage).ThenByDescending(x => x.Goals); this.results.Foreach(x => x.Position = this.results.IndexOf(x));

当两支球队(应该是联合第一名)的比赛获胜率和进球数相同时会出现问题,但是当获得索引时,一支球队将被分配为第一名,另一支球队将被分配为第二名.

The problem arises when two teams (should be joint #1) have the same match win percentage and goals scored but when getting the index one team will be assigned #1 and the other #2.

有没有办法获得正确的位置?

Is there a way to get the correct position?

推荐答案

var position = 1; var last = result.First(); foreach(var team in results) { if (team.WinPercentage != last.WinPercentage || team.Goals != last.Goals) ++position; team.Position = position; last = team; }

更多推荐

根据值获取列表中项目的索引

本文发布于:2023-11-22 20:47:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618960.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   项目   列表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!