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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:06:19+00:00 2026-05-28T00:06:19+00:00

i am calling Sort on a List : List<Stuff> list = new List<Stuff>(); list.Sort(StuffSortByName);

  • 0

i am calling Sort on a List:

List<Stuff> list = new List<Stuff>();
list.Sort(StuffSortByName);

with the StuffSortByName declaration of:

private static int StuffSortByName(Stuff x, Stuff y)
{
    ...
}

What surprises me is that my code compiles and works. It’s surprising that no overload of Sort takes a method:

  • Sort()
  • Sort(Comparison<T>)
  • Sort(IComparer<T>)
  • Sort(Int32, Int32, IComparer<T>)

So i was fortunate, as i didn’t want to have to create a whole object, that implements IComparer, just to sort stuff. But for the life of me i don’t understand while it compiles.


And now i want to duplicate that magic. i want to sort a ListView. But you don’t sort a listivew, you give a listview an IComparer through it’s ListViewItemSorter property:

listView1.ListViewItemSorter = [IComparer]

And, again, i don’t want to write a whole object, i just want to pass a local method (and ideally a non-static one):

listView1.ListViewItemSorter = SortListView;


private static int SortListView(Object x, Object y)
{
    ....
}

Now of course this doesn’t compile because it makes no sense. But then the earlier syntax shouldn’t compile either – but it does.

So it gives me hope that i can have the same confusing syntax here.

How was i allowed to pass a method as a sort comparer in the first case, but not in the second case?

  • 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-28T00:06:20+00:00Added an answer on May 28, 2026 at 12:06 am

    To answer your first question, the reason the method name can be passed is because Comparison<T> is a delegate type, meaning it is a type representing a method signature. See MSDN for details.

    In your case, you should either create an IComparer object or have your Stuff object implement IComparable or IComparable<T> (if possible).

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

Sidebar

Related Questions

Calling Html.RenderPartial(~/Views/Payments/MyControl.ascx); from a view works if MyControl.ascx is a control that directly inherits
I've got some library code that works on a range of .NET runtimes (regular,
I am calling Collections.sort() on an ArrayList using a Comparator that I declared earlier.
Class: Class: private: ... vector<string> words; vector< list<int> > vints; public: myFunction(...) I am
Calling Validate() on an XmlDocument requires passing in a ValidationEventHandler delegate. That event function
Calling the ajax called URL works well without ajax eg. http://localhost/ci/controller/method/param_value . But using
When calling a remote service (e.g. over RMI) to load a list of entities
If I have a list of elements I would like to sort, Java offers
I'm trying to sort each Position (which is a list) in a Position list.
I need to sort a coordinate list for a rectangle counterclockwise, and make the

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.