Sorting an Array of Custom Objects in C#
page 2 of 4
by Sandesh Meda
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 34813/ 43

Method 1: Implementing the IComparable Interface

Step 1: Implement the IComparable interface

Listing 3

class Car : IComparable

Step 2: Define the CompareTo method.

In this example we will be sorting by the Make property of the Car Class.

Listing 4

public int CompareTo(object obj)
  if (obj is Car)
    Car c2 = (Car)obj;
    return Make.CompareTo(c2.Make);
    throw new ArgumentException("Object is not of type Car.");

That is it! We are now ready to test if our sorting works.

Step 3: Test Sorting by Make.

Listing 5

Car objCar = new Car();
ArrayList carArray = new ArrayList();
objCar.Make = "BMW";
objCar.Year = 2008;
objCar.Location = "Florida";
objCar = null;
objCar = new Car();
objCar.Make = "Honda";
objCar.Year = 1996;
objCar.Location = "Illinois";
objCar = null;
objCar = new Car();
objCar.Make = "Corvette";
objCar.Year = 2006;
objCar.Location = "California";
objCar = null;

You will now observe that the carArray is sorted alphabetically by Make.

Note: Sometimes instead of using the ArrayList, it is possible that you are working with an object array. You can use the C# built in Adapter() method as shown below.

To convert from an array to ArrayList use:

ArrayList carArray = ArrayList.Adapter(carObjectArray);

To convert from an ArrayList to object array use:

Car[] carObjectArray = (Car[])carArray.ToArray(typeof(Car));

View Entire Article

User Comments

Title: Great   
Name: peter
Date: 2011-09-02 5:41:56 AM
Great great article
Title: Thanx   
Name: Stef
Date: 2010-02-03 4:10:36 PM
This was a clear explanation..It helped me allot!
Title: nice post   
Name: simi
Date: 2009-12-30 2:13:53 AM

First of all. Thanks very much for your useful post.

I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.

Please let me introduce you some info related to this post and I hope that it is useful for .Net community.

There is a good C# resource site, Have alook

Title: Mast hai   
Name: Niraj kumar
Date: 2009-06-05 8:58:00 AM
Best explanation
Title: Well Done   
Name: jj
Date: 2009-06-03 2:28:54 AM
This is good work
Title: Excelent   
Name: Awais Ranjha
Date: 2009-05-11 4:52:25 PM
This is a great help. i am really thankful to the author. Really excellent article. Thanks a lot!
Title: Coustom object   
Name: shatendra singh
Date: 2009-05-02 6:34:28 AM
this is very well artical.
Title: Great   
Name: Anton
Date: 2009-01-14 2:13:57 PM
Thnx, very helpful
Title: great explanation   
Name: Aga
Date: 2008-11-28 11:50:20 PM
Thanks so much for giving great explanation of the subject!
Title: Useful one   
Name: Chennai Tours Travels
Date: 2008-10-20 3:56:09 AM
Nice explanation Sandesh Meda
Good effort ..
Title: Nice one   
Name: Ravi Sadalagi
Date: 2008-08-12 6:05:44 AM
good one thank you..
Title: Excellent   
Name: Nancy
Date: 2008-08-11 10:23:58 AM
Makes a great attempt to explain sorting an array of custom objects in a very easy understandable way. Really excellent article. Thanks a lot!
Title: Mr   
Name: Amook Vandan
Date: 2008-08-11 4:46:47 AM
good article
Title: good   
Name: Neetu Tiwari
Date: 2008-08-09 6:55:21 AM
Its good.....thanks
Title: Mr   
Name: Priyan R
Date: 2008-07-29 2:18:41 AM
Hi thanks good article

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-07-25 11:49:21 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search