Dictionary Sorting ???
-
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! :)
-
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! :)
Some C# 3 goodness:
string stringWithHighestIntValue = dictionary.OrderBy(input => input.Value).Last().Key;
-
Some C# 3 goodness:
string stringWithHighestIntValue = dictionary.OrderBy(input => input.Value).Last().Key;
-
Some C# 3 goodness:
string stringWithHighestIntValue = dictionary.OrderBy(input => input.Value).Last().Key;
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! :)
-
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! :)
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
-
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
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! :)
-
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
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! :)
-
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! :)
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
-
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
-
Genius!! I so love generics!!! Thanks professor!
Jammer Going where everyone here has gone before! :)
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
-
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! :)
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
-
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