How to sort an array of objects

  • Thread starter Thread starter John Devlon
  • Start date Start date
J

John Devlon

Hi,

I've created my own class containing a few properties like name, zip-code
and savings.
At some point i'm storing several objects in an array.

Does anyone know how to sort the array using the zip-code, again using the
savings and last sorting by name?
(Tripple-sort)

Thanx

John
 
The details (particularly of repeated sorts) is hard to cover in a newsgroup
thread but I will suggest that you need to implement the IComparable
Interface in your custom class. This is the way that the Array.Sort method
knows how to sort things which it has no idea about... it asks your objects
which do know.

If you always want to always sort on the same three properties you can
simply concatenate the values. If on the other hand you want to sort on any
property (or any combination of properties) at any time you have more work.

Hope this helps,
Tom
 
John said:
I've created my own class containing a few properties like name, zip-code
and savings.
At some point i'm storing several objects in an array.

Does anyone know how to sort the array using the zip-code, again using the
savings and last sorting by name?
(Tripple-sort)

As Tom pointed out, if you want custom sorting one of your bets is to
implement the IComparable interface in your class. Something in the
likes of:

<aircode>
'inside your class, say, Person
Implements IComparable

'...

Public Function CompareTo( _
ByVal Obj As Object _
) As Integer _
Implements System.IComparable.CompareTo

'Assumes a ZipCode/Savings/Naming order

Dim Other As Person = TryCast(Obj, Person)
If Other Is Nothing Then Return 1
Dim Result As Integer

Result = Me.ZipCode.CompareTo(Other.ZipCode)
If Result = 0 Then
Result = Me.Savings.CompareTo(Other.Savings)
If Result = 0 Then
Result = Me.Name.CompareTo(Other.Name)
End If
End If
Return Result

End Function
</aircode>


Now, if you need more flexibility, you may consider creating a class
that implements IComparer. The advantage is that most collections
allow you to provide an IComparer that will take precedence over the
IComparable interface of the class being sorted. This gives you the
opportunity to create a completely configurable sorting, just by
passing the appropriate comparer to the sorting routine.

(In the code bellow, I'm assuming you're using VB 2005, which allows
generics, but the principles would be the same for a non-generic
IComparer -- but you'd have to raise exceptions in case of parameters
of types incompatible with your class' type)

<aircode>
Public Class PersonComparer
Implements IComparer(Of Person)

'Assumes a Name/Savings/ZipCode order


Public Function Compare( _
ByVal X As Person, ByVal Y As Person) As Integer _
Implements System.Collections. _
Generic.IComparer(Of Person).Compare

Dim Result As Integer
If X Is Nothing Then
If Y IsNot Nothing Then Result = -1
ElseIf Y Is Nothing Then
Result = 1
Else

Result = String.Compare(X.Name, Y.Name, ignoreCase:=True)

If Result = 0 Then
Result = X.Savings.CompareTo(Y.Savings)
If Result = 0 Then
Result = X.ZipCode.CompareTo(Y.ZipCode)
End If
End If

End If
Return Result
End Function

End Class
</aircode>

HTH.

Regards,

Branco.
 
Back
Top