按(可能是非唯一的)值对Hashtable进行排序 [英] Sort Hashtable by (possibly non-unique) values

查看:70
本文介绍了按(可能是非唯一的)值对Hashtable进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将字符串映射到int的Hashtable.字符串是唯一的,但有些字符串可能映射到相同的整数.

I have a Hashtable that maps strings to ints. Strings are unique, but several may be mapped to the same integer.

我幼稚的方法是将哈希表简单地转换为由哈希表的值索引的SortedList,但是问题是,一旦哈希表的两个字符串映射到相同的值,就会产生冲突.

My naive approach was to simply invert the Hashtable to a SortedList that is indexed by the Hashtable's values, but the problem is that you get a clash as soon as two of the Hashtable's strings map to the same value.

列出按值排序的整个哈希表(键和值)的最有效方法是什么? (在两个值相同的情况下,我不在乎它们的顺序.)

What is the most efficient way to list my entire Hashtable (keys and values) ordered by the values? (Where two values are the same, I don't care about their ordering.)

推荐答案

使用Linq:

hashtable.Cast<DictionaryEntry>().OrderBy(entry => entry.Value).ToList()

这篇关于按(可能是非唯一的)值对Hashtable进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆