Avatar of Thomasian
Thomasian
Flag 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?
.NET ProgrammingEditors IDEsVisual Basic.NET

Avatar of undefined
Last Comment
Thomasian

8/22/2022 - Mon
Jaime Olivares

Thomasian

ASKER
jaime_olivares,

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.
margajet24

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
margajet24

oopss.. late by a few minutes..
Jaime Olivares

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.
Thomasian

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Jaime Olivares

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Thomasian

ASKER
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
        Else
            Return 1
        End If
    End Function
 
    Public Sub New(ByVal ts As TimeSpan)
        _ts = ts
    End Sub
 
End Structure

Open in new window

Jaime Olivares

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.
Thomasian

ASKER
Thanks
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck