Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Have you ever needed to sort a collection that contained data that was not a string or a number? By simply implementing the System.Collections.IComparer interface you have to power to sort any object anyway you want.
Here is an example of sorting an ArrayList of date strings.
ArrayList arr = new ArrayList();
arr.Add("10/1/2003");
arr.Add("09/4/2002");
arr.Add("1/1/2004");
arr.Add("12/1/1999");
arr.Sort(new DateComparer() );
public class DateComparer : System.Collections.IComparer {
public int Compare(Object x, Object y) {
if (x == y) return 0;
if (x == null) return -1;
if (y == null) return 1;
DateTime t1 = DateTime.Parse((string) x);
DateTime t2 = DateTime.Parse((string) y);
return DateTime.Compare(t1, t2);
}
}
The Compare method returns an int that has the following meanings.
Return Value Condition
Is less than zero X is less than y
Zero X equals y
Greater than zero X is greater than y
So how about being able to sort in descending order. Here is an example that allows you to specify the sortOrder in the constructor. I simply added an enum for Ascending/Descending, the appropriate constructors, and a condition to the Compare method to check which way to sort.
public class DateComparer : System.Collections.IComparer {
public enum SortOrder {
Ascending,
Descending
};
private SortOrder _sortOrder;
public DateComparer() {
_sortOrder = SortOrder.Ascending;
}
public DateComparer(SortOrder sortOrder) {
_sortOrder = sortOrder;
}
// Note: Always sorts null entries in the front.
public int Compare(Object x, Object y) {
if (x == y) return 0;
if (x == null) return -1;
if (y == null) return 1;
DateTime t1 = DateTime.Parse((string) x);
DateTime t2 = DateTime.Parse((string) y);
if (_sortOrder == SortOrder.Ascending) {
return DateTime.Compare(t1, t2);
} else {
return DateTime.Compare(t2, t1);
}
}
}
IComparer Interface
https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionsicomparerclasstopic.asp
Comments
- Anonymous
July 31, 2004
Unfortunately, this only works on ArrayList or Array collections (really just arrays). If you try and implement a true collection (based off of iCollection), you don't get a Sort() method).
Check out Rocky's article on how to write your own wrapper for those types of collections:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet01272004.asp