To sort using ArrayList.Sort()

Quoted from http://www.codeproject.com/useritems/sortingarraylist.asp

public
struct MyStrcuture
{
public Int32 iID;
public String sName;
}
In order to be able to sort elements contained in an ArrayList, we have to define the CompareTo method of the IComparable interface, and then use the ArrayList's .Sort() method. This -of course- means that our structure should be inherited from that interface .. and so we'll do!
public struct MyStrcuture : IComparable
{
public Int32 iID;
public String sName;

public Int32 CompareTo(Object obj)
{
MyStrcuture tmpObj = (MyStrcuture) obj;
return (this.sName.CompareTo(tmpObj.sName));
}
}

This would allow the array list to be sorted by just calling MyArrayList.Sort()
provided the arraylist containing the item with the above structure.


Useful sorting algorithms in C#. URL

Comments

Popular posts from this blog

Clearcase check in/uncheck out all the checked out files script

MSSQL GROUP_CONCAT

Duplicating a local copy of project from TFS and making web software client factory to work