解决方案,以排名位置在我的列表
本文关键字:我的 列表 位置 解决方案 | 更新日期: 2023-09-27 18:14:00
考虑以下代码:
class Results
{
public int playerId;
public int score;
public int section;
public int position;
public Results(int _playerId, int _score, int _section)
{
playerId = _playerId;
score = _score;
section = _section;
}
}
public void RankMyResults()
{
List<Results> myResultList = new List<Results>();
myResultList.Add(new Results(1,232, 1));
myResultList.Add(new Results(2,213, 1));
// Add a lot of more results
// Iteriate over the items to set the position
}
我想设置位置1为每个部分的最高分,位置2为第二高,依此类推。
如果两个人得分相同,那么位置应该是这样的
Position Score PlayerId Section
1 135 23 1
1 135 43 1
3 131 45 1
在本例中,它将跳过位置2。
是否有一个很好的方法来使用LINQ来做到这一点,或者例如使用一些选择,排序功能从列表对象?
我自己的解决方案遍历列表一点都不好!
几天前我写了这些扩展方法:
#region RankBy
public static IEnumerable<TResult> RankBy<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, int, TResult> resultSelector)
{
return source.RankBy(keySelector, null, false, resultSelector);
}
public static IEnumerable<TResult> RankBy<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
Func<TSource, int, TResult> resultSelector)
{
return source.RankBy(keySelector, comparer, false, resultSelector);
}
public static IEnumerable<TResult> RankByDescending<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
Func<TSource, int, TResult> resultSelector)
{
return source.RankBy(keySelector, comparer, true, resultSelector);
}
public static IEnumerable<TResult> RankByDescending<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, int, TResult> resultSelector)
{
return source.RankBy(keySelector, null, true, resultSelector);
}
private static IEnumerable<TResult> RankBy<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
bool descending,
Func<TSource, int, TResult> resultSelector)
{
comparer = comparer ?? Comparer<TKey>.Default;
var grouped = source.GroupBy(keySelector);
var ordered =
descending
? grouped.OrderByDescending(g => g.Key, comparer)
: grouped.OrderBy(g => g.Key, comparer);
int totalRank = 1;
foreach (var group in ordered)
{
int rank = totalRank;
foreach (var item in group)
{
yield return resultSelector(item, rank);
totalRank++;
}
}
}
#endregion
#region DenseRankBy
public static IEnumerable<TResult> DenseRankBy<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, int, TResult> resultSelector)
{
return source.DenseRankBy(keySelector, null, false, resultSelector);
}
public static IEnumerable<TResult> DenseRankBy<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
Func<TSource, int, TResult> resultSelector)
{
return source.DenseRankBy(keySelector, comparer, false, resultSelector);
}
public static IEnumerable<TResult> DenseRankByDescending<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
Func<TSource, int, TResult> resultSelector)
{
return source.DenseRankBy(keySelector, comparer, true, resultSelector);
}
public static IEnumerable<TResult> DenseRankByDescending<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, int, TResult> resultSelector)
{
return source.DenseRankBy(keySelector, null, true, resultSelector);
}
private static IEnumerable<TResult> DenseRankBy<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
bool descending,
Func<TSource, int, TResult> resultSelector)
{
comparer = comparer ?? Comparer<TKey>.Default;
var grouped = source.GroupBy(keySelector);
var ordered =
descending
? grouped.OrderByDescending(g => g.Key, comparer)
: grouped.OrderBy(g => g.Key, comparer);
int rank = 1;
foreach (var group in ordered)
{
foreach (var item in group)
{
yield return resultSelector(item, rank);
}
rank++;
}
}
#endregion
你可以这样使用它们:
var rankedPlayers = players.RankByDescending(
p => p.Score,
(p, r) => new { Rank = r, Player = p });
RankBy
和DenseRankBy
之间的区别在于RankBy
产生"间隙"(例如1,1,3,3,3,6…),而DenseRankBy
则没有(1,1,2,2,2,3…)
我修改了上面那些伟大的方法,使它们保持原来的顺序。排序方法不应该改变元素的顺序,它们只应该对元素进行排序,并按照输入集合的原始顺序返回元素。
private static IEnumerable<TResult> RankBy<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
bool descending,
Func<TSource, int, TResult> resultSelector)
{
var comp0 = comparer ?? Comparer<TKey>.Default;
var comp = descending ? Comparer<TKey>.Create((x, y) => -comp0.Compare(x, y)) : comp0;
var keys = source.Select(x => keySelector(x)).ToArray();
var indexes = Enumerable.Range(0, keys.Length).ToArray();
Array.Sort<TKey, int>(keys, indexes, comp);
var groups = new int[keys.Length];
int group = 0;
int index = 0;
for (int j = 1; j < keys.Length; ++j)
{
++index;
if (comp.Compare(keys[j], keys[j - 1]) != 0)
{
group += index;
index = 0;
}
groups[indexes[j]] = group;
}
index = 0;
foreach (var item in source)
{
yield return resultSelector(item, groups[index++] + 1);
}
}
private static IEnumerable<TResult> DenseRankBy<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
bool descending,
Func<TSource, int, TResult> resultSelector)
{
var comp0 = comparer ?? Comparer<TKey>.Default;
var comp = descending ? Comparer<TKey>.Create((x, y) => -comp0.Compare(x, y)) : comp0;
var keys = source.Select(x => keySelector(x)).ToArray();
var indexes = Enumerable.Range(0, keys.Length).ToArray();
Array.Sort<TKey, int>(keys, indexes, comp);
var groups = new int[keys.Length];
int group = 0;
for (int j = 1; j < keys.Length; ++j)
{
if (comp.Compare(keys[j], keys[j - 1]) != 0)
++group;
groups[indexes[j]] = group;
}
int index = 0;
foreach (var item in source)
{
yield return resultSelector(item, groups[index++] + 1);
}
}