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

Outlook : "operation failed, object could not be found. " when trying to close a PST.

Troubleshooting Outlook Express (IMAP, POP3, HTTP, NEWS) with the log file

MSSQL GROUP_CONCAT