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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:08:40+00:00 2026-05-23T15:08:40+00:00

I have a collection of user defined class object, e.g. Car instances, what is

  • 0

I have a collection of user defined class object, e.g. Car instances, what is the best way to sort it.

Below are the possible ways, which one is the best, is there other ways?

  1. IComparable
  2. Comparer
  3. Comparison
  4. IEquatable

Any idea would be very much appreciated

  • 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-23T15:08:41+00:00Added an answer on May 23, 2026 at 3:08 pm

    There’s another important and extremely simple way, unless you absolutely require that you sort in-place: use LINQ to Objects. For example:

    var orderedByName = cars.OrderBy(car => car.Name).ToList();
    

    You can order by multiple projections, and in a varied fashion, like this:

    var complex = cars.OrderByDescending(car => car.Year)
                      .ThenBy(car => car.Model)
                      .ThenByDescending(car => car.Make)
                      .ToList();
    

    I’ve only put the ToList at the end to create a List<Car> at the end – if you only need to iterate over the results though, you can let it perform the ordering lazily:

    var query = cars.OrderByDescending(car => car.Year)
                      .ThenBy(car => car.Model)
                      .ThenByDescending(car => car.Make);
    
    foreach (var car in cars)
    {
        // Use car
    }
    

    Personally I find this the most flexible way of sorting. Otherwise, using IComparer<T> and Comparison<T> are the next most flexible, as anyone can order that way. IComparable<T> requires the class itself to decide how items are going to be ordered, and you can only implement it once, which makes it somewhat inflexible.

    IEquatable<T> is used for comparing items for equality, so is used for things like HashSet<T>, not for ordering.

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

Sidebar

Related Questions

I want to write an user defined error collection class which should collect all
I have multiple Users, each with a collection of Tasks. public class User {
I have a Car class with various properties. I have a static Cars collection
I have a model object which subclasses ActiveRecord. Additionally, using STI, I have defined
I have a user control with a property defined as follows: public partial class
Assume I have a user defined Java class called Foo such as: public class
I have two classes, user and role, defined as: public class User : Entity
I have a user control that contains a collection of controls to be reused
I have created a site collection inside a web application with user A as
I want to have a User Control that takes a collection of People (property

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.