How to sort an array of objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Devlon

    How to sort an array of objects

    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


  • Tom Leylan

    #2
    Re: How to sort an array of objects

    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 Devlon" <johndevlon@hot mail.comwrote in message
    news:ETIHh.4846 7$Ei.130176@pho bos.telenet-ops.be...
    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
    >

    Comment

    • Branco Medeiros

      #3
      Re: How to sort an array of objects

      John Devlon wrote:
      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.ICompara ble.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.Comp areTo(Other.Zip Code)
      If Result = 0 Then
      Result = Me.Savings.Comp areTo(Other.Sav ings)
      If Result = 0 Then
      Result = Me.Name.Compare To(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.Collecti ons. _
      Generic.ICompar er(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:=Tru e)

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

      End If
      Return Result
      End Function

      End Class
      </aircode>

      HTH.

      Regards,

      Branco.

      Comment

      Working...