I created a small test program to gain some experience with the IComparable interface in C#. I need to use the interface for other stuff in future.
In the program I'd like to sort a ArraList of customers based on the salary. Executing my code I always receive the "System.InvalidOperationException" exception.
I already tried different versions of the code and checked a few tutorials, always with the same result.
My code:
using System;
using System.Collections;
namespace CsTest
{
public class Program
{
public static void Main()
{
Customer customer1 = new Customer()
{
ID = 101,
Name = "Ted",
Salary = 4000
};
Customer customer2 = new Customer()
{
ID = 102,
Name = "Tod",
Salary = 7000
};
Customer customer3 = new Customer()
{
ID = 103,
Name = "Tom",
Salary = 5500
};
ArrayList listCustomers = new ArrayList();
listCustomers.Add(customer1);
listCustomers.Add(customer2);
listCustomers.Add(customer3);
Console.WriteLine("Customers before sorting");
foreach (Customer customer in listCustomers)
{
Console.WriteLine(customer.Name + " - " + customer.Salary);
}
// Sort() method should sort customers by salary
listCustomers.Sort();
Console.WriteLine("Customers after sorting");
foreach (Customer customer in listCustomers)
{
Console.WriteLine(customer.Name + " - " + customer.Salary);
}
}
}
public class Customer : IComparable<Customer>
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public int CompareTo(Customer obj)
{
return this.Salary.CompareTo(obj.Salary);
}
}
}
Is there a specific reason you want to use an
ArrayList
instead of aList<Customer>
? A typed List is suggested in general, as it will only accept objects of the specified type.The issue here is that
ArrayList.Sort()
does not use theIComparable
interface. Check the remarks here, where it states that you have to implement theIComparer
interface and use the.Sort(comparer)
overload.This is your one option. The other option (much much easier) is to use a
List<Customer>
and it will use yourIComparable
implementation out of the box. Your choice :)