Link to home
Start Free TrialLog in
Avatar of Thomasian
ThomasianFlag for Philippines

asked on

VB.NET Sorting Collection

I need to get the nth percentile of the values in the collection. To compute the percentile, I first need to sort the values (a property of the object in the collection). Then I can use ElementAt function to get the nth item I need to compute the percentile.

I can use the Sort method but that will be very inefficient because after adding a single item, the whole collection will have to be sorted. I also cannot use the value to be sorted as a key because the value is not unique.

What collection should I use?
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Avatar of Thomasian



Thanks for the suggestion. But as mentioned, the value to be sorted is not unique. SortedList is sorted by it's key and it must be unique.
what is the data of the values?

.. have you tried using SortedList Class? you just have the to specify the IComparer to be used

the difference is, you will specify a key-value pair when you add an item,
and you will access the data using GetKey(index) which is the same as
ElementAt function
oopss.. late by a few minutes..
there are some tricks you can do with the key, if it is of string type, you can append a sequence number after the key, so every key will be different, but still sortable.
It is of timespan type. Objects will only be added/removed from the collection, but never modified.

I can use a SortedDictionary and store the object as the key and specify the IComparable to sort by the timespan. And the value in the collection will not be used.

Or maybe use a List collection and insert new objects in the correct index.
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the code. I now see that the key can have duplicates as long as the comparer says they are not the equal.

I have made some changes.

Dim yourSD As New SortedList(Of TSKey, Boolean)
yourSD.Add(New TSKey(ts), Nothing)

I will store the object as the key and nothing as the value so that I will not be storing the same info twice. I changed the collection to SortedList because it has a keys property where I can get its key while passing its index (don't know why that is not possible with a SortedDictionary).

Is there a more appropriate type of collection for this case?
Structure TSKey
    Implements IComparable(Of TSKey)
    Private _ts As TimeSpan
    Public Function CompareTo(ByVal other As TSKey) As Integer Implements System.IComparable(Of TSKey).CompareTo
        Dim comp As Integer = _ts.CompareTo(other._ts)
        If comp <> 0 Then
            Return comp
            Return 1
        End If
    End Function
    Public Sub New(ByVal ts As TimeSpan)
        _ts = ts
    End Sub
End Structure

Open in new window

there is not a better ready made class, so, I think that's the best you can have, unless you write your own collection class.