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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:54:37+00:00 2026-05-31T17:54:37+00:00

I am writing an address book program. I have each person’s details stored in

  • 0

I am writing an address book program. I have each person’s details stored in a List<Person>. I need to be able to sort this list by last name (using first name if there are ties) or by post code.

So far I have this:

public class Person
{
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string PostCode { get; set; }
    // etc..
}

public class AddressBook
{
    public List<Person> People { get; set; }

    // asc: ascending or descending
    // column: the property to use when sorting
    //         (in my case either LastName or Postcode)
    public void Sort(bool asc, string column)
    {
        // What should I put here?
    }

    // etc...
}

I have tried using the ICompare and IComparable interfaces but I am just not getting it.

How do I write the Sort method?

  • 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-31T17:54:38+00:00Added an answer on May 31, 2026 at 5:54 pm

    You can use an implementation of IComparer<T>:

    public class PersonComparer : IComparer<Person>
    {
        private readonly bool _sortAscending;
        private readonly string _columnToSortOn;
    
        public PersonComparer(bool sortAscending, string columnToSortOn)
        {
            _sortAscending = sortAscending;
            _columnToSortOn = columnToSortOn;
        }
    
        public int Compare(Person x, Person y)
        {
            if(x == null && y == null) return 0;
            if(x == null) return ApplySortDirection(-1);
            if(y == null) return ApplySortDirection(1);
    
            switch(_columnToSortOn)
            {
                case "LastName":
                    return ApplySortDirection(SortByName(x, y));
                    break;
                case "PostCode":
                    return ApplySortDirection(SortByPostCode(x, y));
                    break;
                default:
                    throw new ArgumentOutOfRangeException(
                        string.Format("Can't sort on column {0}",
                        _columnToSortOn));
            }
        }
    
        private int SortByPostCode(Person x, Person y)
        {
            return x.PostCode.CompareTo(y.PostCode);
        }
    
        private int SortByName(Person x, Person y)
        {
            var lastNameResult = x.LastName.CompareTo(y.LastName);
            if(lastNameResult != 0)
                return lastNameResult;
            return x.FirstName.CompareTo(y.FirstName);
        }
    
        private int ApplySortDirection(int result)
        {
            return _sortAscending ? result : (result * -1);
        }
    }
    

    You would use it in the Sort method of your AddressBook class like this, assuming People is a List<Person>:

    public void Sort(bool asc, string column)
    {
        People.Sort(new PersonComparer(asc, column));
    }
    

    This code has the benefit of using an in-place sort.

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

Sidebar

Related Questions

I am writing a program to add to and update an address book. Here
I am writing an app where I need to send all the Address Book
I'm writing this small program to extract any number of email address from a
I am writing an app where I need to read the Address Book data
I am writing an app that interfaces with the iPhone address book. Here is
I'm writing a Firefox extension and I need to find the ip address of
Writing a python program, and I came up with this error while using the
I am learning boost/asio and writing example program which was in e-book. of course
I want to ask a question about the iPhone address book. I am writing
I'm writing python program to build mac-address cache using pcap. But pcap module for

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.