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.

How to transfer app and data from old iPhone to new iPhone for model IOS17 and IOS18 above.

Tips on how to use IBM iNotes (web client) on emailing or scheduling - Like copy tables from Excel or checking for user id (windows logon id) through email address