Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Dictionary Sorting ???

Dictionary Sorting ???

Scheduled Pinned Locked Moved C#
algorithmsquestion
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jammer 0
    wrote on last edited by
    #1

    Hi All, Here's in interesting one. If you have a Dictionary like: String1, 0 String2, 0 String3, 10 String4, 20 How do you locate the key(String4) based on the fact that it has the highest int value (20)? Thanks,

    Jammer Going where everyone here has gone before! :)

    P 1 Reply Last reply
    0
    • J Jammer 0

      Hi All, Here's in interesting one. If you have a Dictionary like: String1, 0 String2, 0 String3, 10 String4, 20 How do you locate the key(String4) based on the fact that it has the highest int value (20)? Thanks,

      Jammer Going where everyone here has gone before! :)

      P Offline
      P Offline
      Professor Sharada Ulhas
      wrote on last edited by
      #2

      Some C# 3 goodness:

      string stringWithHighestIntValue = dictionary.OrderBy(input => input.Value).Last().Key;

      J 2 Replies Last reply
      0
      • P Professor Sharada Ulhas

        Some C# 3 goodness:

        string stringWithHighestIntValue = dictionary.OrderBy(input => input.Value).Last().Key;

        J Offline
        J Offline
        Jammer 0
        wrote on last edited by
        #3

        Genius!! I so love generics!!! Thanks professor!

        Jammer Going where everyone here has gone before! :)

        J 1 Reply Last reply
        0
        • P Professor Sharada Ulhas

          Some C# 3 goodness:

          string stringWithHighestIntValue = dictionary.OrderBy(input => input.Value).Last().Key;

          J Offline
          J Offline
          Jammer 0
          wrote on last edited by
          #4

          Ok, I'm stuck on something else with this dictionary malarky now. Once I've done an evaluation and worked out the highest value in my dictionary and grabbed the string I need to reset all the values in the dictionary to zero. I'm getting errors at the moment saying that the collection has been modified and that it can't enumerate over the collection. So far I've tried:

                  Dictionary.KeyCollection keycoll = FileScore.Keys;
                  foreach (string s in keycoll)
                  {
                      FileScore\[s\] = 0;
                  }
          

          Which fails on the second loop ... and I've tried:

                  foreach (KeyValuePair kvp in FileScore)
                  {
                      FileScore\[kvp.Key\] = 0;
                  }
          

          How am I going to get around this problem of reinitialising the dictionary to hold all zero values?

          Jammer Going where everyone here has gone before! :)

          J 1 Reply Last reply
          0
          • J Jammer 0

            Ok, I'm stuck on something else with this dictionary malarky now. Once I've done an evaluation and worked out the highest value in my dictionary and grabbed the string I need to reset all the values in the dictionary to zero. I'm getting errors at the moment saying that the collection has been modified and that it can't enumerate over the collection. So far I've tried:

                    Dictionary.KeyCollection keycoll = FileScore.Keys;
                    foreach (string s in keycoll)
                    {
                        FileScore\[s\] = 0;
                    }
            

            Which fails on the second loop ... and I've tried:

                    foreach (KeyValuePair kvp in FileScore)
                    {
                        FileScore\[kvp.Key\] = 0;
                    }
            

            How am I going to get around this problem of reinitialising the dictionary to hold all zero values?

            Jammer Going where everyone here has gone before! :)

            J Offline
            J Offline
            Judah Gabriel Himango
            wrote on last edited by
            #5

            Yeah, you can't modify a collection while iterating over it; kind of a chicken-and-egg problem. One easy solution is to iterate over a copy the collection and modify the original:

            foreach (var item in dictionary.ToArray()) // iterate over a copy of the collection
                        {
                            dictionary[item.Key] = 0;
                        }

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: Upon this disciple I'll build my new religion? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

            J 2 Replies Last reply
            0
            • J Judah Gabriel Himango

              Yeah, you can't modify a collection while iterating over it; kind of a chicken-and-egg problem. One easy solution is to iterate over a copy the collection and modify the original:

              foreach (var item in dictionary.ToArray()) // iterate over a copy of the collection
                          {
                              dictionary[item.Key] = 0;
                          }

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: Upon this disciple I'll build my new religion? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

              J Offline
              J Offline
              Jammer 0
              wrote on last edited by
              #6

              That is a bit of a bind isn't it ... I'm just rewriting my code with no dictionary. I'm trying to shoehorn functionality out of the wrong approach basically ... I've just defined a new collection and I'm going to use a List instead. I'm sure I'll find some problems along the way :)

              Jammer Going where everyone here has gone before! :)

              1 Reply Last reply
              0
              • J Judah Gabriel Himango

                Yeah, you can't modify a collection while iterating over it; kind of a chicken-and-egg problem. One easy solution is to iterate over a copy the collection and modify the original:

                foreach (var item in dictionary.ToArray()) // iterate over a copy of the collection
                            {
                                dictionary[item.Key] = 0;
                            }

                Tech, life, family, faith: Give me a visit. I'm currently blogging about: Upon this disciple I'll build my new religion? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                J Offline
                J Offline
                Jammer 0
                wrote on last edited by
                #7

                After some futzing around i'm going to do this I think. I'm performing this each time:

                Dictionary<string, int> FileScoreCopy = new Dictionary<string, int>(FileScore);

                Just out of interest. How do you 'delete' this FileScoreCopy once your done with it? I guess some form of proactive garbage collection.

                Jammer Going where everyone here has gone before! :)

                J 1 Reply Last reply
                0
                • J Jammer 0

                  After some futzing around i'm going to do this I think. I'm performing this each time:

                  Dictionary<string, int> FileScoreCopy = new Dictionary<string, int>(FileScore);

                  Just out of interest. How do you 'delete' this FileScoreCopy once your done with it? I guess some form of proactive garbage collection.

                  Jammer Going where everyone here has gone before! :)

                  J Offline
                  J Offline
                  Judah Gabriel Himango
                  wrote on last edited by
                  #8

                  You don't delete it. When it goes out of scope, it automatically becomes eligible for garbage collection. The .NET garbage collector will reclaim the memory reserved for this object when it runs in the background.

                  Tech, life, family, faith: Give me a visit. I'm currently blogging about: "Religion of Peace" Strikes Jerusalem The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                  J 1 Reply Last reply
                  0
                  • J Judah Gabriel Himango

                    You don't delete it. When it goes out of scope, it automatically becomes eligible for garbage collection. The .NET garbage collector will reclaim the memory reserved for this object when it runs in the background.

                    Tech, life, family, faith: Give me a visit. I'm currently blogging about: "Religion of Peace" Strikes Jerusalem The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                    J Offline
                    J Offline
                    Jammer 0
                    wrote on last edited by
                    #9

                    OK. I need to look into what a destructor is then as that's what I thought they were for. Thanks,

                    Jammer Going where everyone here has gone before! :)

                    J 1 Reply Last reply
                    0
                    • J Jammer 0

                      Genius!! I so love generics!!! Thanks professor!

                      Jammer Going where everyone here has gone before! :)

                      J Offline
                      J Offline
                      Judah Gabriel Himango
                      wrote on last edited by
                      #10

                      Keep in mind the good professor's solution will throw an exception if the dictionary is empty, see the Last() extension method[^] used there. You may want to tweak that code a tad to account for empty dictionaries.

                      Tech, life, family, faith: Give me a visit. I'm currently blogging about: "Religion of Peace" Strikes Jerusalem The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                      1 Reply Last reply
                      0
                      • J Jammer 0

                        OK. I need to look into what a destructor is then as that's what I thought they were for. Thanks,

                        Jammer Going where everyone here has gone before! :)

                        J Offline
                        J Offline
                        Judah Gabriel Himango
                        wrote on last edited by
                        #11

                        Nope, a destructor is only required if you're dealing with unmanaged resources (e.g. closing Win32 files, closing database connections, etc.). For regular managed code where you're not dealing with unmanaged resources, you don't need to implement a destructor.

                        Tech, life, family, faith: Give me a visit. I'm currently blogging about: "Religion of Peace" Strikes Jerusalem The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                        J 1 Reply Last reply
                        0
                        • J Judah Gabriel Himango

                          Nope, a destructor is only required if you're dealing with unmanaged resources (e.g. closing Win32 files, closing database connections, etc.). For regular managed code where you're not dealing with unmanaged resources, you don't need to implement a destructor.

                          Tech, life, family, faith: Give me a visit. I'm currently blogging about: "Religion of Peace" Strikes Jerusalem The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                          J Offline
                          J Offline
                          Jammer 0
                          wrote on last edited by
                          #12

                          Ahhhhhh, that makes total sense now. I was reading about the generations in the GC the other day ... all very interesting! Much to learn!

                          Jammer Going where everyone here has gone before! :)

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups