Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solution to rank positions in my LIST?

Tags:

c#

.net

list

linq

Considering following code:

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
}

I want to set position 1 for the highest score in each section, position 2 for second highest and so on.

Also if two people have the same score the positions should look like this

Position  Score   PlayerId Section
1         135     23       1
1         135     43       1
3         131     45       1

As in this example it will skip position 2.

Is there a nice way to use LINQ to do this or for example using some Select, Sorting functionality from the List Object?

My own solution iterate over the list is not good at all!

like image 252
StefanE Avatar asked Feb 23 '23 11:02

StefanE


1 Answers

I wrote these extension methods just a few days ago:

    #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

You can use them as follows:

var rankedPlayers = players.RankByDescending(
                                p => p.Score,
                                (p, r) => new { Rank = r, Player = p });

The difference between RankBy and DenseRankBy is that RankBy creates "gaps" (e.g. 1,1,3,3,3,6...) whereas DenseRankBy does not (1,1,2,2,2,3...)

like image 150
Thomas Levesque Avatar answered Feb 25 '23 00:02

Thomas Levesque