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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:05:38+00:00 2026-05-17T16:05:38+00:00

How to sort two objects in a list by using two properties one by

  • 0

How to sort two objects in a list by using two properties one by ascending and the other by descending order. when linq is used it says i need to implement IComparer interface but not sure how to compare two objects by using two properties at once.

Say Person class by Name ascending and Age descending.

  • 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-17T16:05:39+00:00Added an answer on May 17, 2026 at 4:05 pm

    Well, you need to decide which is your primary comparison. Only use the secondary comparison if the first one gives equality. For example:

    public int Compare(Person p1, Person p2)
    {
        int primary = p1.Name.CompareTo(p2.Name);
        if (primary != 0)
        {
            return primary;
        }
        // Note reverse order of comparison to get descending
        return p2.Age.CompareTo(p1.Age);
    }
    

    (This can be written more compactly in various ways, but I’ve kept it very explicit so you can understand the concepts.)

    Note that in MiscUtil I have some building blocks so you can easily construct comparers using lambda expressions, compose comparers etc.

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

Sidebar

Related Questions

I have two tables, one that has 450 rows, the other is a Sort
In Python lists, there are two alternative ways of performing a list sort: Using
I am unable to get this code to sort a list of objects using
I have an ArrayList of objects that I need to sort in two different
I am trying to sort a two-dimensional array by a timestamp column Descending. This
how to sort a list in Scala by two fields, in this example I
This is all in C#, using .NET 2.0. I have two lists of objects.
I want to have a stl list of objects where each object contains two
I've got two lists of Customer objects, generated from Entity Framework. I need to
I have a list of objects and Comparator which is used for sorting and

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.