Sort custom C # class dictionary

I have a nested public class KeyCountMap

public KeyCountMap<T> { private IDictionary<T, MutableInt> map = new Dictionary<T, MutableInt>(); public KeyCountMap() { } public KeyCountMap(Type dictionaryType) { if (!typeof(IDictionary<T, MutableInt>).IsAssignableFrom(dictionaryType)) { throw new ArgumentException("Type must be a IDictionary<T, MutableInt>", "dictionaryType"); } map = (IDictionary<T, MutableInt>)Activator.CreateInstance(_dictionaryType); } public HashSet<KeyValuePair<T, MutableInt>> EntrySet() { return map.ToSet(); } //... rest of the methods... } 

To sort the values ​​on the map in descending order of values, if we use Java, we can write a method like:

 public static <T> KeyCountMap<T> sortMapByDescendValue(KeyCountMap<T> map) { List<Entry<T, MutableInt>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, new Comparator<Entry<T, MutableInt>>() { @Override public int compare(Entry<T, MutableInt> o1, Entry<T, MutableInt> o2) { return (-1) * (o1.getValue().get()).compareTo(o2.getValue().get()); } }); KeyCountMap<T> result = new KeyCountMap<T>(); for (Entry<T, MutableInt> entry : list) { result.put(entry.getKey(), entry.getValue()); } return result; } 

If we use C #, we can define the method as:

 public static KeyCountMap<T> SortMapByDescendValue<T>(KeyCountMap<T> map) { List<KeyValuePair<T, MutableInt>> list = new List<KeyValuePair<T, MutableInt>>(map.EntrySet()); // map.EntrySet() returns of type HashSet<KeyValuePair<T, MutableInt>> list = list.OrderByDescending(x => x.Value).ToList(); KeyCountMap<T> result = new KeyCountMap<T>(); foreach (KeyValuePair<T, MutableInt> entry in list) { result.Put(entry.Key, entry.Value); } return result; } 

Will this method work, or do I need to override the CompareTo() method (not used here) for sorting?

EDIT

 public class MutableInt { internal int _value = 1; // note that we start at 1 since we're counting public void Increment() { _value++; } public void Discrement() { _value--; } public int Get() { return _value; } } 
-one
sorting dictionary c # value
Jun 14 '16 at 11:58
source share
2 answers

Dictionaries (hashtables) have no order. Trying to order a hashset by controlling the insertion order just won't work. If you want to order, do not use the dictionary as a backup storage.

+2
Jun 14 '16 at 12:06 on
source share

If you want a sorted dictionary forever, you can try sorting with SortedDictionary<K,V> :

  // Please, notice ...map => new... (C# 6.0 syntax) // since you can't address map in the initializator (=) private IDictionary<T, MutableInt> map => new SortedDictionary<T, MutableInt>( // You are supposed to compare keys Comparer<T>.Create((leftKey, rightKey) => { // given keys, get values MutableInt left = map[leftKey]; MutableInt right = map[rightKey]; //TODO: you may want to change logic here // you should return any positive integer if left > right // negative integer if left < right // zero in case left == right // current implementation (CompareTo) assumes that // MutableInt implements IComparable<MutableInt> interface return -left.CompareTo(right); }) ); 

EDIT : if you want to introduce a dictionary sorted by value, the best way is IMHO for matching values

 public class MutableInt: IComparable<MutableInt> { ... public int CompareTo(MutableInt other) { return (null == other) ? 1 : _value.CompareTo(other._value); } ... } 

And then use Linq:

 //Notice, that you can't return sorted values as dictionary public static IEnumerable<KeyValuePair<T, MutableInt>> SortMapByDescendValue<T>( KeyCountMap<T> map) { return map .OrderByDescending(pair => pair.Value); // Value is comparable now } 

The only thing you cannot do is sort the standard dictionary ( Dictionary<K, V> )

0
Jun 14 '16 at 13:17
source share



All Articles