Sorting a generic List(Of T) using IComparable(Of T) #

List(Of T) supports sorting through it's Sort method.  When you call Sort it will use the default comparer for the items it contans.  So if you have a custom class in there, you need to implement IComparable or IComparable(Of T). 

Here is some code that shows how to do this.  This code sorts lowest to highest.

    Public Function CompareTo(ByVal other As CustomClass) As Integer Implements System.IComparable(Of CustomClass).CompareTo
Dim myscore As Integer = Me.SomeValue
Dim otherscore As Integer = other.SomeValue
If myscore > otherscore Then
Return 1
ElseIf myscore = otherscore Then
Return 0
Else
Return -1
End If
End Function
Categories:  |  | 
Sunday, July 16, 2006 2:15:15 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

All content © 2008, Christopher May, Inc
Open Job Positions
On this page
Google Ads
This site
Calendar
<December 2008>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 1.9.6264.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: