[RESOLVED] Sort an arraylist using compareTo
I have a class with the following function:
Code:
Public Function CompareTo(ByVal obj As Object) As Integer _
Implements System.IComparable.CompareTo
If Not TypeOf obj Is TicketData Then
Throw New Exception("Object is not MyObject")
End If
Dim Compare As TicketData = CType(obj, TicketData)
Dim result As Integer = Me.strSLA.CompareTo(Compare.strSLA)
If result = 0 Then
result = Me.strSLA.CompareTo(Compare.strSLA)
End If
Return result
End Function
Presort I get the following data:
PRESORT
6721.04
3901.46
2768.12
2408.12
795.42
2377.29
2145.21
1866.25
1775.83
after running ticketList.Sort() I get:
SORTED
1.94
102.15
1180
129.58
13.54
133.33
139.17
148.33
1571.88
So it appears that my function is comparing as if it were a string. Can someone help me understand what I'm missing?
Re: Sort an arraylist using compareTo
Dim result As Integer = cdec(Me.strSLA).CompareTo(cdec(Compare.strSLA))
Re: Sort an arraylist using compareTo
What's TicketData? A structure, a class, something more ordinary? What's strSLA?
Re: Sort an arraylist using compareTo
strictly speaking, the vb.net arraylist is obsolete. it was superceded by the list(of T) in vb2005
Re: Sort an arraylist using compareTo
Sorry to not provide all of the details, ticketlist is an arraylist of an object class called ticektdata. Anyways I was trying to sort strSLA which was a string and I needed to convert ithe strSLA string to a decimal using CDec for the operation to work correctly. It works like a charm now, thank you!
Re: [RESOLVED] Sort an arraylist using compareTo
Well.... if it's a decimal value.... why is it being stored as a string?
-tg