本文介绍了根据值获取列表中项目的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该方案适用于足球联赛桌.我可以按比赛获胜率排序,然后按进球数来确定名单在联盟中的位置.然后,我使用此顺序使用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;
 }

这篇关于根据值获取列表中项目的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:06