需要排序字典来查找键小于或大于搜索值的值

发布于 2024-08-29 19:49:03 字数 299 浏览 4 评论 0原文

我需要按价格(十进制)值对对象进行排序,以便快速访问。我需要能够找到价格高于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无声情话 2024-09-05 19:49:05

请考虑一个 SortedList。或者,您可以仅使用任何集合并使用 LINQ 对其进行查询。例如普通的通用列表:

        List<Int32> tempList = new List<Int32>();

        tempList.Where(singleItem => singleItem > 100)
            .ToList<Int32>();

Please think of a SortedList. Alternatively you can use just any collection and query it with LINQ. For example plain generic List:

        List<Int32> tempList = new List<Int32>();

        tempList.Where(singleItem => singleItem > 100)
            .ToList<Int32>();
小清晰的声音 2024-09-05 19:49:04

您可以在 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.

早茶月光 2024-09-05 19:49:04

答案取决于您的使用模式。如果这是消耗未排序的输入集并查找合适的对象的一次性练习,那么您最好使用 LINQ:

list.Where(e => A < e.Price || e.Price < B);

如果列表是静态的,并且您想要查询多个范围,则将对象粘贴到数组中,然后排序按价格进行分类,然后使用二元印章来查找感兴趣的范围。

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:

list.Where(e => A < e.Price || e.Price < B);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文