none
How to use Dictionary.OrderByDescending<,> RRS feed

  • Question

  • I need to sort the  integer key in Descending order.I also need to how to iterate thru this dictionary after sorting.
    Might be a Silly question. I am new to C#.
    Thanks in Advance.
    Sunday, November 29, 2009 11:39 AM

Answers

  • Hi,
    Here is a sample

    Hope it helps.

                Dictionary<int, string> d = new Dictionary<int, string>();
                d.Add(5, "Foo5");
                d.Add(1, "Foo1");
                d.Add(3, "Foo3");
                var result= d.OrderByDescending(i => i.Key);
                //or
                IEnumerable<KeyValuePair<int, string>> result1 = d.OrderByDescending(i => i.Key);
                foreach (KeyValuePair<int, string> kvp in result)
                {
                    Console.WriteLine(kvp.Value);
                }
                Console.WriteLine("Second Option");
                foreach (KeyValuePair<int, string> kvp in result1)
                {
                    Console.WriteLine(kvp.Value);
                }
    • Marked as answer by eryang Friday, December 4, 2009 3:30 AM
    Sunday, November 29, 2009 12:05 PM

All replies

  • Hi,
    Here is a sample

    Hope it helps.

                Dictionary<int, string> d = new Dictionary<int, string>();
                d.Add(5, "Foo5");
                d.Add(1, "Foo1");
                d.Add(3, "Foo3");
                var result= d.OrderByDescending(i => i.Key);
                //or
                IEnumerable<KeyValuePair<int, string>> result1 = d.OrderByDescending(i => i.Key);
                foreach (KeyValuePair<int, string> kvp in result)
                {
                    Console.WriteLine(kvp.Value);
                }
                Console.WriteLine("Second Option");
                foreach (KeyValuePair<int, string> kvp in result1)
                {
                    Console.WriteLine(kvp.Value);
                }
    • Marked as answer by eryang Friday, December 4, 2009 3:30 AM
    Sunday, November 29, 2009 12:05 PM
  • Hi,

    This document shows more details about Dictionary in .NET, hope it can help.

     

    Thanks,

    Eric


    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    Tuesday, December 1, 2009 9:24 AM