需要排序字典来查找键小于或大于搜索值的值
我需要按价格(十进制)值对对象进行排序,以便快速访问。我需要能够找到价格高于 A 或低于 B 的所有物品。我正在考虑 SortedList,但它没有提供一种从给定键值开始查找升序或降序枚举器的方法(比如给我所有价格低于 120 美元的对象)。
想象一个系统,它接受卖家出售的汽车并将其存储到该集合中。然后买家希望找到价格低于 1000 美元的汽车。
基本上我需要的是基于树的集合和功能来查找小于\大于\等于提供的密钥的节点。
请指教。
I need to have objects sorted by price (decimal) value for fast access. I need to be able to find all objects with price more then A or less than B. I was thinkg about SortedList, but it does not provide a way to find ascending or descending enumerator starting from given key value (say give me all objects with price less than $120).
Think of a system that accepts cars for sale from sellers and stores them into that collection. Then buyers want to find cars cheaper than $1000.
Basically what i need is tree-based collection and functionality to find node that is smaller\greater\equal to provided key.
Please advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请考虑一个 SortedList。或者,您可以仅使用任何集合并使用 LINQ 对其进行查询。例如普通的通用列表:
Please think of a SortedList. Alternatively you can use just any collection and query it with LINQ. For example plain generic List:
您可以在 BinarySearch 上使用 < a href="http://msdn.microsoft.com/en-us/library/system.collections.sortedlist.aspx" rel="nofollow noreferrer">SortedList 搜索满足您的条件的第一个和最后一个索引然后只需从列表中获取项目范围。
You can use BinarySearch on SortedList to search first and last indexes satisfying your conditions and then just get range of items from list.
答案取决于您的使用模式。如果这是消耗未排序的输入集并查找合适的对象的一次性练习,那么您最好使用 LINQ:
如果列表是静态的,并且您想要查询多个范围,则将对象粘贴到数组中,然后排序按价格进行分类,然后使用二元印章来查找感兴趣的范围。
The answer depends on your usage patterns. If this a one-off exercise of consuming an unsorted input set and finding suitable objects, you are much better off just using LINQ:
If the list is static, and you want to query multiple ranges, then stick the objects into an array, sort them by price, and then use a binary chop to find ranges of interest.