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

  • SEARCH
  • Home
  • 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 7676113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:07:26+00:00 2026-05-31T17:07:26+00:00

I have a class that wraps a List and adds functionality for dealing with

  • 0

I have a class that wraps a List and adds functionality for dealing with ratios. It needs to be sorted two ways at different times; by ordinal position, and by the amount of the ratio.

Below is the method I am currently using to do the sort by ordinal position which I want to use as the basis for two related questions:

  1. Is there a more perfomant way to do this?
  2. How would I do this using an IComparer?

The primary motivation for passing in the IComparer would be to allow for different implementations to randomly shuffle the list. This post seems to address the same issue but without any resolution in code that I can see. I’m looking for some sample code and usage of it so I can wrap my head around it.

Cheers,
Berryl

/// <summary>
/// Maintains a list of ratios suitable for use in an allocation when the
/// sum of all items is exactly equal to one (100%). No individual it may
/// be less than zero or greater than one.
/// </summary>
public class RatioBag : IList<RatioAssociation>
{
    private readonly IList<RatioAssociation> _items = new List<RatioAssociation>();

    private Random _rnd;

    public void Order(IndexOrdering ordering) {
        IList<RatioAssociation> ordered = null;
        switch (ordering)
        {
            case IndexOrdering.FirstToLast:
                break;
            case IndexOrdering.LastToFirst:
                ordered = this.Reverse().ToList();
                break;
            case IndexOrdering.Random:
                if (_rnd == null) _rnd = new Random();
                ordered = Enumerable.Range(0, Count)
                    .OrderBy(x => _rnd.Next())
                    .Select(index => this[index])
                    .ToList();
                break;
            default:
                throw new ArgumentOutOfRangeException("ordering");
        }
        if (ordered == null) return;

        Clear();
        foreach (var ra in ordered) {
            Add(ra);
        }
    }
  • 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-31T17:07:27+00:00Added an answer on May 31, 2026 at 5:07 pm

    The performance of your approach is not optimal, because you aren’t doing an in place sort. Instead you are copying each item twice. First in the call to ToList and then in the call to Add.

    The IComparer interface is used in combination with the List.Sort method that does an in place sort. So that should be the way to go.


    You would first need to change the type of _items to List<RatioAssociation>.

    For each ordering you want to support, you would need to create a new class that implements IComparer. If these classes wouldn’t be used else where you could put them as private classes inside RatioBag.

    Your Order method would then look something like this:

    public void Order(IndexOrdering ordering)
    {
        switch (ordering)
        {
            case IndexOrdering.FirstToLast:
                _items.Sort(new AscendingComparer());
                break;
            case IndexOrdering.LastToFirst:
                _items.Sort(new DescendingComparer());
                break;
            case IndexOrdering.Random:
                _items.Sort(new RandomComparer());
                break;
            default:
                throw new ArgumentOutOfRangeException("ordering");
        }
    }
    

    However, I would suggest to remove the IndexOrdering enum and provide one ordering method per ordering type. The code would be cleaner.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that wraps List<> I have GetValue by index method: public
I have class that represents users. Users are divided into two groups with different
I have a custom iterator template class that wraps up a std::list iterator. In
In Java, say you have a class that wraps an ArrayList (or any collection)
I have a class called DatabaseHelper that wraps a DbConnection. What's the proper way
I have class method that returns a list of employees that I can iterate
I have a class called Device that accepts two policies as far as I
I want to create a class which wraps a list of structures. I have
I have a class that needs a property set inside a LINQ-to-SQL query. My
I have an object that has a few different List properties that contain child

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.