Alex Rivera | Logout

Getting i-th value from a SortedList or SortedDictionary

Asked 2008-10-24T16:09:57.320
23

I have a sorted collection of objects (it can be either SortedList or SortedDictionary, I will use it mainly for reading so add performance is not that important). How can I get the i-th value?

So e.g. when I have numbers 1, 2, 3, 4, 5 in the collection and I want the median (so 3 in this example), how can I do it?

Edit
Report

1 Answer

9

Try something like this:

list.Values[list.Count / 2];

Note that a true median would average the two numbers in the middle if Count is even.

answered 2008-10-24T16:14:42.490

Your Answer