views:

257

answers:

3

Hi

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.

+5  A: 

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.

Marcelo Cantos
I don't need closed range, just less OR greater
Captain Comic
That's a fairly simple change.
Marcelo Cantos
That is indeed. I am reluctant to use LINQ due to performance issues.
Captain Comic
Don't optimise prematurely. LINQ code is quite fast enough for most uses, and can even outperform hand-written list-manipulation logic in some cases.
Marcelo Cantos
+1  A: 

You can use BinarySearch on SortedList to search first and last indexes satisfying your conditions and then just get range of items from list.

Alex
+1  A: 

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>();
Piotr Justyna