I can sort an ArrayList of Strings. How can I sort an ArrayList of Objects? Can it be done? Yes, you can do it with IComparable. I know some are so enthused about Generics, they have made statements like, "I will never use an ArrayList again." But ArrayLists are still very powerful and have their place. This application will show you why.
This week I am working on an application that has to sort a relatively small number of Input Lines (300 or so) on Social Security Number because there can be multiple lines of input for the same person. The application makes a call to a Web Service for each person but should only make one call per person. The processing of the application requires that I build objects prior to beginning the processing. Since the input file can have multiple lines for the same person, the objects must be sorted.
Even if the first Property of the data class is the SSN, you cannot just place the objects in an ArrayList and then sort the ArrayList. Your class must Implement IComparable and you must creat an IComparable.CompareTo function in the class. This article will show you the code for a Console Application in both C# and VB.NET to accomplish the sorting.
Figure 1 - C# Sorting ArrayList of Objects.
| using System; using System.Collections; namespace SortArrayListOfObjects { class Class1 { static ArrayList al = new ArrayList(); [STAThread] static void Main(string[] args) { BuildArrayList(); ListSortedObjects(); //list data before sorting al.Sort(); ListSortedObjects(); Console.ReadLine(); } static private void BuildArrayList() { MyObject obj1 = new MyObject(); obj1.EmpID = 1; obj1.SSN = "234567890"; obj1.Name = "Les Smith"; al.Add(obj1); MyObject obj2 = new MyObject(); obj2.EmpID = 2; obj2.SSN = "987654321"; obj2.Name = "Bob Jones"; al.Add(obj2); MyObject obj3 = new MyObject(); obj3.EmpID = 3; obj3.SSN = "123456789"; obj3.Name = "Jim Jones"; al.Add(obj3); } static private void ListSortedObjects() { foreach (MyObject obj in al) { Console.WriteLine(obj.SSN); Console.WriteLine(obj.EmpID.ToString()); Console.WriteLine(obj.Name); } } } public class MyObject : IComparable { public string SSN; public int EmpID; public string Name; public int CompareTo(object obj) { MyObject Compare = (MyObject)obj; int result = this.SSN.CompareTo(Compare.SSN); if (result == 0) result = this.SSN.CompareTo(Compare.SSN); return result; } } } |
| Imports System Imports System.Collections Module Module1 Private al As New ArrayList Sub Main() BuildArrayList() ListSortedObjects() al.Sort() ListSortedObjects() Console.ReadLine() End Sub Private Sub BuildArrayList() Dim obj1 As MyObject = New MyObject obj1.EmpID = 1 obj1.SSN = "234567890" obj1.Name = "Les Smith" al.Add(obj1) Dim obj2 As MyObject = New MyObject obj2.EmpID = 2 obj2.SSN = "987654321" obj2.Name = "Bob Jones" al.Add(obj2) Dim obj3 As MyObject = New MyObject obj3.EmpID = 3 obj3.SSN = "123456789" obj3.Name = "Jim Jones" al.Add(obj3) End Sub Private Sub ListSortedObjects() Dim obj As MyObject For Each obj In al Console.WriteLine(obj.SSN) Console.WriteLine(obj.EmpID.ToString()) Console.WriteLine(obj.Name) Next End Sub End Module Public Class MyObject Implements IComparable Public SSN As String Public EmpID As Integer Public Name As String Public Function CompareTo(ByVal obj As Object) As Integer _ Implements System.IComparable.CompareTo If Not TypeOf obj Is MyObject Then Throw New Exception("Object is not MyObject") End If Dim Compare As MyObject = CType(obj, MyObject) Dim result As Integer = Me.SSN.CompareTo(Compare.SSN) If result = 0 Then result = Me.SSN.CompareTo(Compare.SSN) End If Return result End Function End Class |
| 234567890 1 Les Smith 987654321 2 Bob Jones 123456789 3 Jim Jones |
| 123456789 3 Jim Jones 234567890 1 Les Smith 987654321 2 Bob Jones |
| Ask a Question, or give your feedback on my articles or products by clicking on My Blog. | ![]() |