The folowing code sample creates a generic SortedList
A System.Collections.Generic.IComparer to sort based on DateTime in reverse order:
...
using System.Collections;
using System.Collections.Generic;
...
public class DecendingDateCompare : IComparer
{
public int Compare(DateTime x, DateTime y)
{
return x.CompareTo(y) * -1;
}
}
A sample code to test:
...
using System.Collections;
using System.Collections.Generic;
...
SortedList
new SortedList
aSortedList.Add(DateTime.Now.AddDays(-10), 100);
aSortedList.Add(DateTime.Now.AddDays(10), 10);
aSortedList.Add(DateTime.Now.AddDays(-5), 90);
aSortedList.Add(DateTime.Now.AddDays(5), 20);
aSortedList.Add(DateTime.Now.AddDays(-3), 80);
aSortedList.Add(DateTime.Now.AddDays(3), 30);
StringBuilder sb = new StringBuilder();
IDictionaryEnumerator iDicEnum =
(IDictionaryEnumerator) aSortedList.GetEnumerator();
while( iDicEnum.MoveNext() )
sb.AppendLine( iDicEnum.Key.ToString() +
", " +
iDicEnum.Value.ToString() );
MessageBox.Show(sb.ToString());
No comments:
Post a Comment