You can create your won custome IComparer implementation to control sort order. While IComparable.CompareTo method controls the default sort order for a class, IComparer.Compare can be used to provide custom sort order.
For e.g. following class which implements IComapere, provides custom reverse sort
For e.g. following class which implements IComapere, provides custom reverse sort
public class reverseSort : IComparer
{
int IComparer.Compare(Object y, Object x)
{
return ((new CaseInsensitiveComparer()).Compare(x, y));
}
}
//test class
ArrayList al = new ArrayList();
al.AddRange(new String[] {"This", "is", "a", "Amazing", "world", "of", "Collections" });
//default sort order ascending
al.Sort();
foreach (object o in al)
{
Console.WriteLine(o.ToString());
}
//custom sort order
al.Sort(new reverseSort());
foreach (object o in al)
{
Console.WriteLine(o.ToString());
}
Comments