Click to See Complete Forum and Search --> : Sorting a dictionary.
MikeB
November 8th, 2008, 09:44 AM
I need to sort a dictionary by value. I have the following code:
public void Sort()
{
List<KeyValuePair<string, PluginDomain>> list = new List<KeyValuePair<string, PluginDomain>>(_domains);
list.Sort(delegate(KeyValuePair<string, PluginDomain> a, KeyValuePair<string, PluginDomain> b)
{
return a.Value.Ordinal.CompareTo(b.Value.Ordinal);
});
The problem with the above, is that it sorts the list just fine, how to I copy the contents of the list back to the dictionary?
Do I have to iterate through the list and add to the dictionary one object at a time or is there a better way?
Mike B
Mutant_Fruit
November 8th, 2008, 09:52 AM
You can't sort a dictionary by value. It makes no sense. Dictionaries are for finding a value *really fast* based on a key. What are you trying to accomplish?
MikeB
November 8th, 2008, 10:19 AM
You can't sort a dictionary by value. It makes no sense. Dictionaries are for finding a value *really fast* based on a key. What are you trying to accomplish?
I don't see why this doesn't make sences, this isn't a sorted dictionary and there is no defined order of keys in a regular dictionary object.
I want to be able to refer to an object in the dictionary by name (which is the key) but I also want to order the items by value for listing the objects in the GUI.
Mike
MadHatter
November 8th, 2008, 10:49 AM
I don't see why this doesn't make sences, this isn't a sorted dictionary and there is no defined order of keys in a regular dictionary object.
there is a defined order of keys in a dictionary. they are ordered by their hash code (ie key.GetHashCode)
what you're attempting to use is a sort of map. since there's no map data structure, why not create your own? its the sort of basic stuff they make you do in school anyway so it shouldn't be that difficult.
Mutant_Fruit
November 8th, 2008, 11:16 AM
I don't see why this doesn't make sences, this isn't a sorted dictionary and there is no defined order of keys in a regular dictionary object.
There is no defined order *of the values*. It is entirely implementation specific. If you want to be able to order the values alphabetically (or something like that), then you need a different data structure. A dictionary is the wrong thing completely to be using.
TheCPUWizard
November 8th, 2008, 12:29 PM
In general when you want to be able to access data by two difference means, the best solution is two different datastructures.
This is common enough that I have developed a set of general classes that contain one (or more) dictionaries for searching (multiple keys) and one or more lists for ordered accessing.
Given that (typically) the items are reference tyype items, there is little memory overhead.
The "big" question is when to sort the list:
a) Agressively (on insert, delete or change)
b) Lazy (on retrieval)
[my classes contain a flag which controls this behaviour....]
toraj58
November 10th, 2008, 02:54 AM
it is not possible to sort a dictionary (Dictionary<keyType, valueType>) but you can use a trick,
you can copy the contents of the dictionary to a generic list (List<string>) and then implement a Comparison delegate and sorting method for the list then sorting it.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.