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 301297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:59:34+00:00 2026-05-12T06:59:34+00:00

I want to implement some various algorithms for practice, just to see how bad

  • 0

I want to implement some various algorithms for practice, just to see how bad I really am and to get better :p

Anyways, I thought I would try to use IEnumerable<T> and IOrderedEnumerable<T> and other .Net collection types just to be compatible (so that what I write can be used more easily later).

But I can’t find a way to return an instance of IOrderedEnumerable<T> other than using the OrderBy and ThenBy extension methods. So I guess I have to create my own class that implements this interface. But the interface doesn’t quite make sense to me to be honest. It might, but I’m not sure.

I created an empty class, added the interface and then got ReSharper to add empty implementations for me. It looks like this:

class MyOrderedEnumerable<T> : IOrderedEnumerable<T>
{
    /// <summary>
    /// Performs a subsequent ordering on the elements of an <see cref="T:System.Linq.IOrderedEnumerable`1"/> according to a key.
    /// </summary>
    /// <returns>
    /// An <see cref="T:System.Linq.IOrderedEnumerable`1"/> whose elements are sorted according to a key.
    /// </returns>
    /// <param name="keySelector">The <see cref="T:System.Func`2"/> used to extract the key for each element.</param><param name="comparer">The <see cref="T:System.Collections.Generic.IComparer`1"/> used to compare keys for placement in the returned sequence.</param><param name="descending">true to sort the elements in descending order; false to sort the elements in ascending order.</param><typeparam name="TKey">The type of the key produced by <paramref name="keySelector"/>.</typeparam><filterpriority>2</filterpriority>
    public IOrderedEnumerable<T> CreateOrderedEnumerable<TKey>(Func<T, TKey> keySelector, IComparer<TKey> comparer, bool descending)
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Returns an enumerator that iterates through the collection.
    /// </summary>
    /// <returns>
    /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
    /// </returns>
    /// <filterpriority>1</filterpriority>
    public IEnumerator<T> GetEnumerator()
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Returns an enumerator that iterates through a collection.
    /// </summary>
    /// <returns>
    /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
    /// </returns>
    /// <filterpriority>2</filterpriority>
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

What I don’t understand is the CreateOrderedEnumerable method. What exactly is it meant to do? Well, I guess it of course would create an ordered enumerable, but how? Is the sorting algorithm itself supposed to go in there? And what will it sort? There is no collection of items going in to that method, so where is it meant to get the collection to order? How would you use the class? Is it meant to be implemented as for example a private helper class inside something that needs to sort stuff?

Then instead of a MyOrderedEnumerable<T> : IOrderedEnumerable<T>, you might have a QuickSorter<T> : IOrderedEnumerable<T> that took a collection in its constructor and sorted it when that CreateOrderedEnumerable method was called… but what would then happen if someone called GetEnumerator and started to enumerate before that method had been called?


Haha, just discovered I had asked something similar a while ago here. But that was just about if it was possible to return one. So I guess this question is a response to the one answer I got there =)

  • 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-12T06:59:34+00:00Added an answer on May 12, 2026 at 6:59 am

    I have a sample implementation you could look at. It’s not designed to be efficient by any means, but it should get you started.

    Basically an IOrderedEnumerable<T> just needs to have an idea of its current ordering, so it can create a new one. Assuming you already have an IComparer<T> you build a new one by saying something like:

    int Compare(T first, T second)
    {
        if (baseComparer != null)
        {
            int baseResult = baseComparer.Compare(first, second);
            if (baseResult != 0)
            {
                return baseResult;
            }
        }
        TKey firstKey = keySelector(first);
        TKey secondKey = keySelector(second);
    
        return comparer.Compare(firstKey, secondKey);        
    }
    

    So basically you create a chain of comparers going from the “least significant” up to the “most significant”. You also need to put the “descending” bit in there, but that’s easy 🙂

    In the sample linked above, the three different aspects are represented in three different classes already present in MiscUtil:

    • ReverseComparer: reverses an existing IComparer<T>‘s results
    • LinkedComparer: creates one comparer from two, with one master and one slave
    • ProjectionComparer: creates a comparer based on a projection from the original items to keys, delegating to another comparer to compare those keys.

    Comparers are great for chaining together like this.

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

Sidebar

Ask A Question

Stats

  • Questions 209k
  • Answers 209k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use a sort descriptor: NSSortDescriptor * sortByScore = [[[NSSortDescriptor alloc]… May 12, 2026 at 9:46 pm
  • Editorial Team
    Editorial Team added an answer Based on the latest information you added to your question,… May 12, 2026 at 9:46 pm
  • Editorial Team
    Editorial Team added an answer No you can't. You can use TimeZone.getAvailableIDs(int rawoffset) to narrow… May 12, 2026 at 9:46 pm

Related Questions

This is going to be a little long and rambly, but I want to
I have a fairly simple sync problem. I have a table with about 10
I want to implement file uploading with behaviour which is commonly seen in various
I want to implement forms authentication on an ASP.NET website, the site should seek

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.