R
Robert Robbins
Hello Programmers,
I cannot figure out how to use the IComparer interface to sort an array
based on the value of a double. I want to sort an array of dealers based on
their distance from a zip code. The error this code gives me is "Specified
IComparer threw an exception ." on the last line of code shown below. -
Robert Robbins (Web Developer)
Imports System.Collections
' a data structure for a dealer object
Public Class Dealer
Public strName As String
Public strAddress As String
Public strCity As String
Public strState As String
Public strZip As String
Public strPhone As String
Public dblDistance As Double
End Class
Public Class ValueComparer
Implements IComparable
Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements IComparable.CompareTo
If TypeOf obj Is Dealer Then
Dim temp As Dealer = CType(obj, Dealer)
Return obj.dblDistance.CompareTo(temp.dblDistance)
End If
Throw New ArgumentException("object is not a Dealer")
End Function
End Class
' code that attempts to sort the array
Dim arrayOfDealers(100) As Dealer
' sort the array of dealers
Dim tmp As ValueComparer()Array.Sort(arrayOfDealers, tmp)
I cannot figure out how to use the IComparer interface to sort an array
based on the value of a double. I want to sort an array of dealers based on
their distance from a zip code. The error this code gives me is "Specified
IComparer threw an exception ." on the last line of code shown below. -
Robert Robbins (Web Developer)
Imports System.Collections
' a data structure for a dealer object
Public Class Dealer
Public strName As String
Public strAddress As String
Public strCity As String
Public strState As String
Public strZip As String
Public strPhone As String
Public dblDistance As Double
End Class
Public Class ValueComparer
Implements IComparable
Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements IComparable.CompareTo
If TypeOf obj Is Dealer Then
Dim temp As Dealer = CType(obj, Dealer)
Return obj.dblDistance.CompareTo(temp.dblDistance)
End If
Throw New ArgumentException("object is not a Dealer")
End Function
End Class
' code that attempts to sort the array
Dim arrayOfDealers(100) As Dealer
' sort the array of dealers
Dim tmp As ValueComparer()Array.Sort(arrayOfDealers, tmp)