IComparer - How Do I Use It?

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)
 
S

solex

Robert,

I would implement the interface in side your dealer class other wise you
cannot compare to anything.

For instance

Imports System.Collection

Public Class Dealer
Implements IComparable

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

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 Me.dblDistance.CompareTo(temp.dblDistance)
End If

Throw New ArgumentException("object is not a Dealer")

End Function
End Class
 
A

Armin Zingler

Robert Robbins said:
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)


The signature you are using is

ByVal keys As System.Array, ByVal items As System.Array

tmp can point to an array of ValueComparer objects, but you neither create
an array nor a ValueComparer object. What you probably intend to do is:

Dim tmp As New ValueComparer
Array.Sort(arrayOfDealers, tmp)

But: You haven't created a dealer so far, so you have to create them and add
them to the array before you can call the Sort method.
 
R

Robert Robbins

Thanks,

I have implemented the interface inside my Dealer class but now I'm not
sure how to call the array sort. Using "Array.Sort(arrayOfDealers)"
generates an "Object reference not set to an instance of an object." error
as soon as I attempt to reference any array element.

Robert Robbins
Web Developer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top