我有一个嵌套的公共类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...
}
要按值的降序排序map中的值,如果我们使用Java,我们可以将方法编写为:
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;
}
如果我们使用C#,我们可以将方法定义为:
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;
}
此方法是否有效或是否需要覆盖CompareTo()
方法(此处未使用)进行排序?
编辑
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;
}
}
答案 0 :(得分:2)
字典(哈希表)没有订单。尝试通过控制插入顺序来订购哈希集只是不起作用。如果您想订购,请不要使用字典作为后备商店。
答案 1 :(得分:0)
如果您想要永久排序字典,可以尝试使用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);
})
);
编辑:如果你想代表按价值排序的字典,最好的方法是恕我直言,使值可比较
public class MutableInt: IComparable<MutableInt>
{
...
public int CompareTo(MutableInt other)
{
return (null == other)
? 1
: _value.CompareTo(other._value);
}
...
}
然后使用 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
}
你唯一不能做的就是排序标准字典(Dictionary<K, V>
)