System.IComparable

This interface provides a CompareTo method which is used to determine the sort order of the implementing types.
Provides a default sort order
This interface is implemented by types that can be sorted, strings for example can be alphabetized, any given string can be compared with another string to determine which should come first in an ordered list.
Provides a method of comparing two objects of a particular type.
Great for ordering of simple objects


Which Interface ?

IComparable - Provides a comparer check (for sorting) when there is only on way of ordering the objects (implemented inside the class)
IComparer - Allows you to define and use multiple comparer checks (implemented outside the class)


CompareTo Method

You must implement this method


int IComparable.CompareTo(object obj) 
{
   myobject my = (myobject)obj;
   return String.Compare(this.MySortProperty, myobject.MySortProperty)
}

In this example String.Compare is used because the property used for comparison is a string.



© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

PrevNext