Solved

One more sort method needed

Posted on 2009-07-11
2
253 Views
Last Modified: 2012-05-07
I have a compareTo method that sorts on lastname and then firstname

What if I want anohter way to be able to sort? I want them both but be able to choose.....where and how would that look like?. Right now this is my sort method but I would like to add one that does firstname first and then second name.

A generic collection in your application to be ordered in two different ways one
use of IComparer <T>.   (The Icomapare<T> is done)
#region IComparable<Contact> Members
 
       
        public int CompareTo(Contact other)
        {
            int result = this.LastName.CompareTo(other.LastName);
            if (result == 0)
            {
                result = this.FirstName.CompareTo(other.FirstName);
            }
            return result;
        }
 
 
        #endregion

Open in new window

0
Comment
Question by:Mickeys
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 24833550
Here's one way to do it...
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            List<Contact> Contacts = new List<Contact>();
            
            Contact ctA = new Contact();
            ctA.FirstName = "Idle";
            ctA.LastName = "Mind";
            Contacts.Add(ctA);
 
            Contact ctB = new Contact();
            ctB.FirstName = "Indiana";
            ctB.LastName = "Jones";
            Contacts.Add(ctB);
 
            Contact ctC = new Contact();
            ctC.FirstName = "Alpha";
            ctC.LastName = "Zebra";
            Contacts.Add(ctC);
 
            Contact.SortOrder = Contact.ContactSortOrder.FirstNameThenLastName;
            Console.WriteLine(Contact.SortOrder.ToString());
            Contacts.Sort();
            foreach (Contact ct in Contacts)
                Console.WriteLine(ct.ToString());
 
            Console.WriteLine();
            Contact.SortOrder = Contact.ContactSortOrder.LastNameThenFirstName;
            Console.WriteLine(Contact.SortOrder.ToString());
            Contacts.Sort();
            foreach (Contact ct in Contacts)
                Console.WriteLine(ct.ToString());
        }
 
    }
 
    public class Contact : IComparable<Contact>
    {
 
        public enum ContactSortOrder { LastNameThenFirstName, FirstNameThenLastName };
        public static ContactSortOrder SortOrder = ContactSortOrder.LastNameThenFirstName;
 
        public string LastName;
        public string FirstName;
 
        public int CompareTo(Contact other)
        {
            if (Contact.SortOrder == ContactSortOrder.LastNameThenFirstName)
            {
                int result = this.LastName.CompareTo(other.LastName);
                if (result == 0)
                {
                    result = this.FirstName.CompareTo(other.FirstName);
                }
                return result;
            }
            else
            {
                int result = this.FirstName.CompareTo(other.FirstName);
                if (result == 0)
                {
                    result = this.LastName.CompareTo(other.LastName);
                }
                return result;
            }
        }
 
        public override string ToString()
        {
            return this.FirstName + " " + this.LastName;
        }
    }

Open in new window

0
 
LVL 29

Assisted Solution

by:Göran Andersson
Göran Andersson earned 250 total points
ID: 24833610
To get different sortings, you can add a comparer to the Contact class. Then you can use that in the Sort method:

contact.Sort(new Contact.Comparer(Contact.SortOrder.FirstNameLastName));

public class Contact {
 
   public enum SortOrder { FirstNameLastName, LastNameFirstName }
 
   public class Comparer : IComparer<Contact> {
 
      private SortOrder _sortOrder;
 
      public Comparer(SortOrder sortOrder) {
         _sortOrder = sortOrder;
      }
 
      public int Compare(Contact x, Contact y) {
         int result;
         switch (_sortOrder) {
            case SortOrder.FirstNameLastName:
               result = x.FirstName.CompareTo(y.FirstName);
               if (result == 0) {
                  result = x.LastName.CompareTo(y.LastName);
               }
               break;
            case SortOrder.LastNameFirstName:
               result = x.LastName.CompareTo(y.LastName);
               if (result == 0) {
                  result = x.FirstName.CompareTo(y.FirstName);
               }
               break;
            }
         return result;
      }
 
   }
 
   ...
 
}

Open in new window

0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to reference objects of the calling class without passing them as parameters 29 176
rest webservice call over https via c# 6 41
Service Controller 1 23
HttpPostedFile 1 26
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question