By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,253 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,253 IT Pros & Developers. It's quick & easy.

IComparer - How Do I Use It?

P: n/a
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)
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
"Robert Robbins" <rr******@kolbnetworks.com> wrote in message
news:vp************@corp.supernews.com...
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)

Nov 20 '05 #2

P: n/a
"Robert Robbins" <rr******@kolbnetworks.com> schrieb
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.
--
Armin

http://learn.to/quote

Nov 20 '05 #3

P: n/a
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

"solex" <so***@nowhere.com> wrote in message
news:e4****************@TK2MSFTNGP12.phx.gbl...
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
"Robert Robbins" <rr******@kolbnetworks.com> wrote in message
news:vp************@corp.supernews.com...
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)


Nov 20 '05 #4

P: n/a
* "Robert Robbins" <rr******@kolbnetworks.com> scripsit:
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. -


<http://www.mvps.org/dotnet/dotnet/samples/codingtechnique/downloads/CompareOperator.zip>

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

P: n/a
Robert,

Check out Herfried's post, I may have mislead you.

Dan

"Robert Robbins" <rr******@kolbnetworks.com> wrote in message
news:vp************@corp.supernews.com...
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

"solex" <so***@nowhere.com> wrote in message
news:e4****************@TK2MSFTNGP12.phx.gbl...
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
"Robert Robbins" <rr******@kolbnetworks.com> wrote in message
news:vp************@corp.supernews.com...
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)



Nov 20 '05 #6

P: n/a
Hi Robert,

Here are a couple of previous threads asking about IComparer, complete
with examples.
http://tinyurl.com/sk7g

Regards,
Fergus
Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.