We need a sorted dictionary designed to search for values โ€‹โ€‹with keys that are less or greater than the search value

I need to have objects sorted by price (decimal) value for quick access. I need to find all objects with a price of more than A or smaller than the Bed and . I was thinking of a SortedList , but it does not provide a way to search for an ascending or descending enumerator, starting with a given key value (say, give me all objects with a price of less than $ 120).

Think of a system that accepts cars for sale from sellers and stores them in this collection. Then buyers want to find cars cheaper than $ 1,000.

Basically I need a tree-based collection and a function to find a node that is less than \ greater than \ equal to the provided key.

Please advice.

+3
source share
3 answers

The answer depends on your usage patterns. If this is a one-time exercise to consume an unsorted set of input data and search for suitable objects, you are much better off using LINQ:

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

If the list is static and you want to request multiple ranges, then paste the objects into an array, sort them by price, and then use binary chasing to find the ranges of interest.

+4
source
+5

Please think of a SortedList. Alternatively, you can use only any collection and query it using LINQ. For example, the usual general list:

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

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

Source: https://habr.com/ru/post/1742447/


All Articles