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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:03:15+00:00 2026-05-18T20:03:15+00:00

I have a person class with three fields, Title, Name, Gender and I would

  • 0

I have a person class with three fields, Title, Name, Gender and I would like to create a Custom Sort for it to sort it first by Title, then by Name and then by Gender ascending:

public class SortPerson : IComparer
    {
        public int Compare(object x, object y)
        {
            (…)
        }
    }

I know how to do this for only one variable to compare against:
But How would I have to proceed with three?

public class SortPerson : IComparer
        {

int IComparer.Compare(object a, object b)
   {
      Person p1=(Person)a;
      Person p2=(Person)b;
      if (p1.Title > p2.Title)
         return 1;
      if (p1.Title < p2.Title)
         return -1;
      else
         return 0;
   }
}

Many Thanks,

  • 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-18T20:03:16+00:00Added an answer on May 18, 2026 at 8:03 pm
    //Assuming all the fields implement IComparable
    int result = a.field1.CompareTo(b.field1);
    if (result == 0)
      result = a.field2.CompareTo(b.field2);
    if (result == 0)
      result = a.field3.CompareTo(b.field3);
    return result;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a person class with three fields, Title, Name, Gender and I would
I have a Struts + Velocity structure like for example, a Person class, whose
For example I have a simple class like public class Person{ public int Age
Say I have a .NET class like so: public class Person { public string
I have models (simplified example): class Group(models.Model): name = models.CharField(max_length = 32) class Person(models.Model):
I have a model class like this: class Person { string FirstName, string LastName,
I have a class Person which can have several Homes, each one with one
Here is the domain that I wish to have: public class Person { public
I have the following classes: public class Person { public String FirstName { set;
For the sake of simplicity, let's say I have a Person class in Python.

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.