views:

223

answers:

3

I have an object in a list that I need to rank several different ways. Currently the code is rather unwieldy as it requires me to individually address each column. Example:

public class Data
{
    public int AValue { get; set; }
    public int ARanking { get; set; }
    public int BValue { get; set; }
    public int BRanking { get; set; }
    public int CValue { get; set; }
    public int CRanking { get; set; }
}

public class Container
{
    public List<Data> RankingData { get; set; }

    public void RankData()
    {
        int count = 1;

        foreach (Data item in RankingData.OrderBy(d => d.AValue))
        {
            item.ARanking = count;
            count++;
        }

        count = 1;

        foreach (Data item in RankingData.OrderBy(d => d.BValue))
        {
            item.BRanking = count;
            count++;
        }

        count = 1;

        foreach (Data item in RankingData.OrderBy(d => d.CValue))
        {
            item.CRanking = count;
            count++;
        }
    }
}

The problem I am trying to solve is I want to write something roughly like this:

public void RankData<V, R>()
{
    int count = 1;

    foreach(Data item in RankingData.OrderBy(V))
    {
        item.R = count;
        count++;
    }
}

So that as I need to alter the ranking logic (for example, handle tie breaking rules) that I write the code once instead of copying the code 20 times to make the rules match. What am I missing?

UPDATE

Using Tanzelax's solution as a base this is the extension class I came up with:

public static class RankingExtension
{
    public static void SetRanking<TKey>(this List<Data> dataSet, bool Ascending, Func<Data, TKey> getOrderBy, Action<Data, int> setRank)
        where TKey : IComparable
    {
        var ordered = (Ascending) ? dataSet.OrderBy(getOrderBy) : dataSet.OrderByDescending(getOrderBy);

        int i = 1;
        foreach (Data item in ordered)
        {
            setRank(item, i);
            i++;
        }
    }
}

I had to add in a switch so that I could control whether or not the field was being sorted ascending or not. And in my test scenarios it produces the appropriate output:

    List<Data> test = new List<Data>();
    test.Add(new Data { AValue = 25, BValue = 1.25, CValue = 99.99 });
    test.Add(new Data { AValue = 89, BValue = 2.10, CValue = 1.01 });
    test.Add(new Data { AValue = 10, BValue = 6, CValue = 45.45 });
    test.Add(new Data { AValue = 15, BValue = 2.33, CValue = 2.99 });
    test.Add(new Data { AValue = 90, BValue = 5.43, CValue = 27.89 });

    test.SetRanking(false, d => d.AValue, (d, i) => d.ARank = i);
    test.SetRanking(false, d => d.BValue, (d, i) => d.BRank = i);
    test.SetRanking(true, d => d.CValue, (d, i) => d.CRank = i);
+1  A: 

Not tested, but something like this:

void SetRanking(this List<Data> dataSet, Func<Data,int> getOrderBy, Action<Data,int> setRank)
{
    var ordered = dataSet.OrderBy(getOrderBy).ToArray();

    int i = i;
    foreach (Data item in ordered)
    {
        setRank(item, i);
        i++;
    }
}

RankingData.SetRanking(d => d.AValue, (d,i) => d.ARanking = i);
RankingData.SetRanking(d => d.BValue, (d,i) => d.BRanking = i);
RankingData.SetRanking(d => d.CValue, (d,i) => d.CRanking = i);
Tanzelax
Your code wasn't quite there, but it got me pretty damn close. I'll post my modification in a bit.
thaBadDawg
@thaBadDawg: Glad I could help. :) Could also generic out the Data and whatnot. Also, instead of passing an ascending bool, you could simply pass negative CValue in the orderBy lambda, though I'm sure a lot of people would object to that approach.
Tanzelax
A: 

Pass in a Func<Data,K> that returns the ranking key. K should implement IComparable

public static void Rank<K>( IEnumerable<Data> source, Func<Data,K> rankBy ) where K : IComparable
{
   int count = 1;
   foreach (var item in source.OrderBy( rankBy ))
   {
       item.R = count;
       ++count;
   }
}
tvanfosson
+1  A: 

This is similar to Tanzelax's answer but is a generic extension method.

public static void RankData<TSource, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector,
    Action<TSource, int> rankSetter
)
{
    int count = 1;
    foreach (var item in source.OrderBy(keySelector))
    {
        rankSetter(item, count);
        ++count;
    }
}

It would be called similar to Tanzelax's answer also.

RankingData.RankData(d => d.AValue, (d,i) => d.ARanking = i);
DRBlaise