sorted lists are normally sorted by key not value in ascending order. this is how to create a sorted list by value in descending order
class DescendingComparer : IComparer
{
public int Compare(object x, object y)
{
try
{
return System.Convert.ToInt32(x).CompareTo(System.Convert.ToInt32(y)) * -1;
}
catch (Exception ex)
{
return x.ToString().CompareTo(y.ToString());
}
}
}
class KeySortedList : SortedList
{
public SortedList ReversedList = new SortedList(new DescendingComparer());
public new void Add(object key, object value)
{
ReversedList.Add(value, key);
base.Add(key, value);
}
}
Wednesday, August 13, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment