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]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to