none
Sorting dictionary by any value RRS feed

  • Question

  • Hello, I've been stuck on this one since a few days, I'm trying to make objects with multiple properties and then implement them into dictionaries which can be sorted for a chosen property. I've gotten as far as this:

    int A1 = 100;
                int A2 = 50;
                int A3 = 200;
                int A4 = 150;


                SortedDictionary<int, TimberSections> players2 = new SortedDictionary<int, TimberSections>();

                players2.Add(A1, new TimberSections() { Name = "EX1", Class = "C16", Thickness = 38, Width2 = 140 });
                players2.Add(A2, new TimberSections() { Name = "EX2", Class = "C16", Thickness = 38, Width2 = 184 });
                players2.Add(A3, new TimberSections() { Name = "IL1", Class = "C16", Thickness = 38, Width2 = 89 });
                players2.Add(A4, new TimberSections() { Name = "IN1", Class = "C16", Thickness = 38, Width2 = 89 });

                foreach (KeyValuePair<int, TimberSections> p in players2)
                {
                    Console.WriteLine("{0}\t{1}", p.Key, p.Value);
                }

                Console.ReadKey();

    This allows me to sort the value according to the key. Is there any way i can sort this for one of my object properties (Name,Class,Thickness,Width2)? Maybe dictionary is the wrong tool?

    Any help would be welcome,

    Thanks

    Saturday, January 12, 2019 12:12 PM

Answers

  • The sorteddictionary is the wrong tool for this. It only sorts on the Key, but it doesn't sort on an arbitrary field of the Value.

    You can simply use a List<T>, and then use the Linq extensions to sort it on any field:

    List<TimberSections> myList = new List<TimberSections>();
    
    myList Add(new TimberSections() { Name = "EX1", Class = "C16", Thickness = 38, Width2 = 140 });
    myList.Add(new TimberSections() { Name = "EX2", Class = "C16", Thickness = 38, Width2 = 184 });
    myList.Add(new TimberSections() { Name = "IL1", Class = "C16", Thickness = 38, Width2 = 89 });
    myList.Add(new TimberSections() { Name = "IN1", Class = "C16", Thickness = 38, Width2 = 89 });
    
    // sort on the field "Name" (for instance):
    var sortedList = myList.OrderBy(t => t.Name);
    
    foreach (var p in sortedList)
    {
        Console.WriteLine("{0} {1}", p.Name, p.Class); // or whatever you want to print
    }


    Saturday, January 12, 2019 2:15 PM
    Moderator
  • var sortedList = myList.OrderBy(t => t.Name).ThenBy(t=>t.Class);
    • Marked as answer by matty1248 Saturday, January 12, 2019 11:47 PM
    Saturday, January 12, 2019 9:09 PM
    Moderator

All replies

  • The sorteddictionary is the wrong tool for this. It only sorts on the Key, but it doesn't sort on an arbitrary field of the Value.

    You can simply use a List<T>, and then use the Linq extensions to sort it on any field:

    List<TimberSections> myList = new List<TimberSections>();
    
    myList Add(new TimberSections() { Name = "EX1", Class = "C16", Thickness = 38, Width2 = 140 });
    myList.Add(new TimberSections() { Name = "EX2", Class = "C16", Thickness = 38, Width2 = 184 });
    myList.Add(new TimberSections() { Name = "IL1", Class = "C16", Thickness = 38, Width2 = 89 });
    myList.Add(new TimberSections() { Name = "IN1", Class = "C16", Thickness = 38, Width2 = 89 });
    
    // sort on the field "Name" (for instance):
    var sortedList = myList.OrderBy(t => t.Name);
    
    foreach (var p in sortedList)
    {
        Console.WriteLine("{0} {1}", p.Name, p.Class); // or whatever you want to print
    }


    Saturday, January 12, 2019 2:15 PM
    Moderator
  • Great, thanks for that Alberto, it felt like i was so close with dictionaries that i forgot my other options. 

    Thanks again, 

    Matthew

    Saturday, January 12, 2019 4:48 PM
  • By the way, is it also possible to sort by a second parameter, such that if the property is shared on the first parameter then it will fall through to the second?

    Saturday, January 12, 2019 4:51 PM
  • var sortedList = myList.OrderBy(t => t.Name).ThenBy(t=>t.Class);
    • Marked as answer by matty1248 Saturday, January 12, 2019 11:47 PM
    Saturday, January 12, 2019 9:09 PM
    Moderator
  • Great, much appreciated,

    Matthew

    Saturday, January 12, 2019 11:47 PM