Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 646701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:38:29+00:00 2026-05-13T21:38:29+00:00

I have an object in a list that I need to rank several different

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:38:29+00:00Added an answer on May 13, 2026 at 9:38 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of object that need to be displayed and moveable on
I have two List's that contain a object of Tag like List i need
I have a list of IDs for objects that I need to grab, then
I have a list of Object arrays (List) That I am going to pass
I have a list object List<Documents> , and inside that I have another list
I have a list of object that is the data source. Something like that:
I have an ASP.NET WebService that returns an object of List public class Students
I have a Result object that lets me pass around a List of event
I have a parent object called Page that has a List of objects called
I have a list of class items List<MyClass> I have a seperate object that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.