Question SortedList(Of TKey, TValue) (IComparer(Of TKey)) usage

ebortoluzzi

Member
Joined
Sep 17, 2009
Messages
8
Programming Experience
Beginner
Hi all :),

I'm creating a sorted list of the type:

Dim ExtractionList As New SortedList(Of Date, Integer())
Dim Estrazioni(7) As Integer
Dim DataKey As Date

using the

ExtractionList.Add(DataKey, Estrazioni)

I have a sorted list according to the DataKey from the oldest date (1st element) to the latest date (last element), because I did not wrote the IComparer the default IComparer(of Date) as been used.

But what I want is a reverse logic of sorting : from the latest date (1st element) to the oldest date (last element).

So I read that I can define the rule at the creation of the list, using the constructor:
SortedList(Of TKey, TValue) (IComparer(Of TKey))

Here is where I got lost the only example in VB is related to strings:
Dim openWith As New SortedList(Of String, String)( _
StringComparer.CurrentCultureIgnoreCase)

but as a DateComparer does not exist I do not know how to proceed; this comparer stuff is new for me can you help me, by:
1 - writing examples
2 - address to examples
....

thank you :D
 
The general class is SortedList(Of TKey, TValue). Your specific instance is a SortedList(Of Date, Integer()), therefore you're fixing TKey to be Date and TValue to be Integer(). Now, in order to perform a custom sort of your keys you need an object that implements IComparer(Of TKey), but you have fixed TKey to be Date so you need an object that specifically implements IComparer(Of Date).

Here's my blog post on the topic of creating an IComparer and using it for sorting purposes. Your situation is only a tiny bit different. Once you have the class you need to pass an instance to your SortedList constructor instead of to a Sort method.
 
Well first of all thank you for your replay John:eek:

I was hoping in something simple, let's see if your string example is enough

I can try to redefine it from String to Date class

Public Class DateLatest2Old
Implements IComparer(Of Date)
Public Function Compare(ByVal x As date, _
ByVal y As Date) As Integer _
Implements IComparer(Of Date).Compare


'write code in order to
'Return -1 x<y or 0 x=y or 1 x>y
'example
return DateAndTime.DateDiff(DateInterval.Day, x, y)

End Function
End Class

Is it a correct way ?

then how I can use it with the SortedList(of date,integer)? as fa as I understood should be

Dim ExtractionList As New SortedList(Of Date, Integer())( DateLatest2Old.compare)

:rolleyes:
 
Actually you can use the default comparison, just reverse it to compare y with x instead of x with y:
VB.NET:
Return y.CompareTo(x)
The parameter to the constructor is an instance of this class:
VB.NET:
Dim ExtractionList As New SortedList(Of Date, Integer())([B]New DateLatest2Old[/B])
 
This is from my blog:
Our StringLengthComparer.Compare method will take two Strings and return a result that indicates their relative order based on their Lengths. Notice that we are still making use of the IComparable.CompareTo method, which we know is a good convention to follow. In this case we are comparing the two Length properties, which are type Int32. The Int32 structure implements the IComparable interface so we should make use of it.
In this case you are comparing two Dates and the Date type implements IComparable too, so you should using the CompareTo method of one of the Dates to compare it to the other one.

As for using it, you're supposed to create an instance of your class and pass that to the SortedList constructor.
 
Back
Top