sorting array of structures using CompareTo

here2learn

New member
Joined
Mar 30, 2006
Messages
3
Programming Experience
10+
Hi,

has anyone got a simple solution to sorting an array of structures using the IComparable.Compare interface and the array.CompareTo method ?

the structure contains an integer and double field, I want to sort on the double field

any help ? cheers :confused:
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,605
Location
Norway
Programming Experience
10+
Example:
VB.NET:
Public Structure Player
Implements IComparable
  Dim playername As String
  Dim hiscore As Short
 
  Public Function compareto(ByVal obj As Object) As Integer _
  Implements IComparable.CompareTo
    Return Me.hiscore.CompareTo(DirectCast(obj, Player).hiscore)
  End Function
 
End Structure
If you create a Players array of type Player here, Array.Sort(Players) will sort ascending on the Short member hiscore.
 
Top Bottom