.NET Tip: Sort an ArrayList Using a Custom Comparer Class Lansdale PA

Provide your own comparison algorithm when sorting by implementing the IComparer interface.

Local Companies

CPA Technology, LLC
610-862-1998
101 W. Elm Street
Conshohocken, PA
The MediGroup, Ltd.
610-666-1955 x12
1308 Egypt Road
Oaks, PA
Deacom, Inc.
610-971-2278
950 West Valley Road, Suite 3000
Wayne, PA
Quintiq Inc.
610-964-8111
565 E Swedesford Rd #303
Wayne, PA
KasTech Consulting, Inc.
215-702-8155
116 N. Bellevue Avenue
Langhorne, PA
Helpline Center Office- Childrens Aid Society
(215) 361-6942
306 N Madison Ave
Lansdale, PA
Mousley Consulting
(215) 631-1366
608 W Main St
Lansdale, PA
Orsino Consulting
(215) 362-6302
Lansdale, PA
Adept Consulting Services Inc
(215) 855-3610
408 W Main St
Lansdale, PA
Idea Revolution
(215) 855-7045
531 S Broad St
Lansdale, PA

provided by: 
Originally published at Internet.com


The Sort() method of the ArrayList class allows you to provide your own comparer. When you provide a comparison object, your Compare() method will be used to compare objects in the ArrayList when sorting. This flexibility allows you to sort by any means that you choose. This example will sort an ArrayList that contains objects of different types. The Compare() method sorts them based on the name of the data type of the object. First, take a look at the DataTypeComparer class that implements the IComparer interface and defines a Compare() method that does a comparison based on the names of the data types of two objects. public class DataTypeComparer : IComparer { public DataTypeComparer() {} public int Compare(object obj1, object obj2) { return obj1.GetType().ToString(). CompareTo(obj2.GetType().ToString()); } }

To use the new DataTypeComparer, you need to create an ArrayList, fill it with data, and then pass an instance of DataTypeComparer to the Sort() method of the ArrayList. Here is some sample code that creates an ArrayList and populates it with five different data types. It prints the data types and values of the unsorted items, sorts them using a DataTypeComparer object, and then prints the sorted items. ArrayList Items = new ArrayList(); // Add several different data types to the Items ArrayList Items.Add(5); Items.Add("C# Tip"); Items.Add(new object()); Items.Add(new DateTime()); Items.Add(9876543210); // Print out the unsorted list of items Debug.Print("Unsorted:\r\nData Type [Value]"); foreach (object item in Items) Debug.Print(" " + item.GetType().ToString() + " [" + item.ToString() + "]"); // Sort the items DataTypeComparer MyDataTypeComparer = new DataTypeComparer(); Items.Sort(MyDataTypeComparer); // Print out the list of items sorted by data type Debug.Print("Sorted:\r\nData Type [Value]"); foreach (object item in Items) Debug.Print(" " + item.GetType().ToString() + " [" + item.ToString() + "]");

Here is the output from executing the code above: Unsorted: Data Type [Value] System.Int32 [5] System.String [C# Tip] System.Object [System.Object] System.DateTime [1/1/0001 12:00:00 AM] System.Int64 [9876543210] Sorted: Data Type [Value] System.DateTime [1/1/0001 12:00:00 AM] System.Int32 [5] System.Int64 [9876543210] System.Object [System.Object] System.String [C# Tip]

You can see from the final output that the items are sorted by the data type of the item in the ArrayList. Try your hand at creating a custom comparer class that sorts on something other the data type.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

CPA Technology, LLC

Sage software, consulting & implementation, training (Sage Authorized Training Center)

610-862-1998
101 W. Elm Street
Conshohocken, PA
www.cpatechnology.net

CPA Technology is a leader in providing Sage software solutions to the construction and real estate industries in Eastern Pennsylvania, New Jersey, and Delaware.

www.cpatechnology.net

Related Local Events
Hot Java at the Watercooler
Dates: 12/23/2009 - 1/23/2010
Location: Odyssey Travel
Skippack, PA
View Details

IT Roundtable
Dates: 11/10/2009 - 11/10/2009
Location: D&E Communications
Brownstown, PA
View Details

Fraser Advanced Information Systems Mixer
Dates: 10/29/2009 - 10/29/2009
Location:
Bethlehem, PA
View Details

PREP 2009: 22nd International Symposium, Exhibit and Workshops
Dates: 7/19/2009 - 7/22/2009
Location: Loews Hotel
Philadelphia, PA
View Details