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

The Archive Base Latest Questions

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

I want to write a generic method with the following signature : IList<T> Sort<T>

  • 0

I want to write a generic method with the following signature :

IList<T> Sort<T> (IList<T> list) where T: IComparable <T> 

that returns a sorted list.
sorry for the incomplete original post.
so I want to sort the list and then select the first n elements
that would be

        List<T> temp = new List<T>(list);
        temp.Sort();
        List<T> temp2 = new List<T>(temp);
        temp2.Take(count);

the complete question would be how to do that without double – copying the initial list.
there would be 2 cases :
the list has dupes and I want to retrieve the first n distinct values
the list has dupes and I want to retrieve the first n values.
for the first case a distinct should be applied also – so a new “third “copy of list to be avoided.

of course the answer posted by guffa is accepted, because the OP at first was incomplete.

  • 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-25T21:56:39+00:00Added an answer on May 25, 2026 at 9:56 pm

    Create a new List<T> from the input and sort it:

    public IList<T> Sort<T> (IList<T> list) where T: IComparable <T> {
      List<T> temp = new List<T>(list);
      temp.Sort();
      return temp;
    }
    

    The Sort method will use the default comparer when you don’t specify one, which uses the IComparable<T> implementation if there is one.

    Edit:

    To answer the edited question:

    You have to copy the list twice if you want to preserve the input and return a list.

    You could get around the second copying if you return IEnumerable<T> instead of IList<T>. Then you can return a deferred result that reads from the first copy of the list. The drawback is of course that it will keep the entire first list in memory although you only use a part of it.

    Anyway, the version returning an IList<T> would be something like:

    public IList<T> Sort<T> (IList<T> list, int cnt, bool distinct) where T: IComparable <T> {
      IEnumerable<T> temp = list.OrderBy(t => t);
      if (distinct) {
        temp = temp.Distinct();
      }
      return temp.Take(cnt).ToList();
    }
    

    For the version returning an IEnumerable<T> you would just not do the .ToList() in the last step.

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

Sidebar

Related Questions

I am trying to write a generic method that would allow me to pass
Suppose I want to author a generic Max method which returns the maximum of
I want to write a common method which returns any model like product, sales,etc.
I get following compilation errors: The method sort(List<T>, Comparator<? super T>) in the type
I'm trying to write generic method to cast types. I want write something like
I want to write a generic function to calculate factorial in C# ... like:
I want to write in Delphi (2009 - so I have generic dictionary class)
I want to write a program in Prolog that confirms if a b-tree of
I want to write a procedure to perform a delete for the following i
I want to invoke a method on a COM object that is defined in

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.