Your right this is a wee bit confusing but after doing it once it should
be no biggie :) Can the classes be on the same .vb page?
sort.vb
------------
Public Class AnObjectThatImplementsIComparer
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object)
As Integer Implements System.Collections.IComparer.Compare
Dim pb As AnObject
pb = CType(x, AnObject)
Return pb.CompareTo(y)
End Function
End Class
Public Class AnObject
Implements IComparable
Public Score as Double
Public Function CompareTo(ByVal obj As Object) As Integer
Implements System.IComparable.CompareTo
Dim pb1 As AnObject = CType(obj, AnObject)
'This should sort us in descending order
Return pb1.Score.CompareTo(Me.Score)
End Function
End Class
------------
In doing that I'm getting some errors under the Public Class AnObject.
It doesn't either of the Implements
I'm somewhat new to the more advanced programming with VB but really
want to learn. I appreciate your help.
Thank you,
Robert Hoenig
Business System Analyst
Johann A. Krause, Inc.
305 W Delavan Drive
Janesville, WI 53547
Phone: +1 (608) 741-4863
Fax: +1 (608) 757-7066
E-Mail: [EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]>
Web: www.gilmanassembly.com
<blocked::http://www.gilmanassembly.com/>
________________________________
From: [email protected]
[mailto:[EMAIL PROTECTED] On Behalf Of kindawords
Sent: Wednesday, June 15, 2005 10:09 AM
To: [email protected]
Subject: [AspNetAnyQuestionIsOk] Re: ArrayList Sorting
This is one of those things that makes sense once you've done it a few
times, but until it clicks it is a wee bit confusing.
You need a class that implements IComparer, which is an interface with
one method, Compare.
Here is an example
Public Class AnObjectThatImplementsIComparer
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object)
As Integer Implements System.Collections.IComparer.Compare
Dim pb As AnObject
pb = CType(x, AnObject)
Return pb.CompareTo(y)
End Function
End Class
This compare method is used by the sort call to compare the value of
two objects in the collection you are sorting. As you notice it
simply calls the CompareTo method on one of the objects. The object,
by the way must implement the IComparable interface.
Public Class AnObject
Implements IComparable
Public Score as Double
Public Function CompareTo(ByVal obj As Object) As Integer
Implements System.IComparable.CompareTo
Dim pb1 As AnObject = CType(obj, AnObject)
'This should sort us in descending order
Return pb1.Score.CompareTo(Me.Score)
End Function
End Class
As you notice this method calls CompareTo as well, and this simply
compares the .Score properties on both objects.
So when you call .Sort on your collection
Dim SortObject as AnObjectThatImplementsIComparer = new
AnObjectThatImplementsIComparer
ACollection.Sort(SortObject)
your collection gets sorted.
--- In [email protected], "Hoenig, Robert"
<[EMAIL PROTECTED]> wrote:
> Can someone please help me with sorting an array list. I added
> arrlist.sort() and now it wants the ICompare. How do I implement the
> ICompare class?? Some simple sample code would be greatly
appreciated.
>
> Thank you,
> Robert Hoenig
> Business System Analyst
>
> Johann A. Krause, Inc.
> 305 W Delavan Drive
> Janesville, WI 53547
> Phone: +1 (608) 741-4863
> Fax: +1 (608) 757-7066
> E-Mail: [EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED]>
> Web: www.gilmanassembly.com
> <blocked::http://www.gilmanassembly.com/>
>
>
> This email has been scanned by the Gilman Engineering &
Manufacturing, LLC
> Email Security System.
>
>
> [Non-text portions of this message have been removed]
________________________________
Yahoo! Groups Links
* To visit your group on the web, go to:
http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/
* To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]
cribe>
* Your use of Yahoo! Groups is subject to the Yahoo! Terms of
Service <http://docs.yahoo.com/info/terms/> .
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
This email has been scanned by the Gilman Engineering & Manufacturing, LLC
Email Security System.
[Non-text portions of this message have been removed]
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/