Thursday, July 8, 2010

Reverse .NET SortedList

 This post shows how you could reverse a SortedList in C# by its keys:

SortedList<string, string> strlist = new SortedList<string, string>();
strlist.Add("f3", "xxx");
strlist.Add("f1", "ccc");
strlist.Add("f2", "aaa");

foreach (KeyValuePair<string, string> kvp in strlist)
{
    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}           

Console.WriteLine("reverse the list");
IEnumerable<KeyValuePair<string, string>> revList = strlist.Reverse();
foreach (KeyValuePair<string, string> kvp in revList)
{
    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}




No comments:

Post a Comment