realbasic-nug
[Top] [All Lists]

Re: How do you sort Object arrays?

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: How do you sort Object arrays?
From: Phil M <phil at mobleybros dot com>
Date: Fri, 29 Sep 2006 17:28:36 -0700
Delivered-to: listarchive at realsoftware dot com
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <C491F54F-2391-4E85-B2D1-6F856F4ADF15 at mobleybros dot com> <561CF0CA-F9FA-4A8E-A730-5B65B25E58EB at mac dot com> <33EFC3AC-6422-4085-8D8C-55047260898F at mac dot com>
On Sep 29, 2006, at 5:06 PM, John Kubie wrote:

I knew I should have thought about it a bit more. My method will fail if there are multiple objects with identical sort elements; adding a second one to the dictionary will erase the first. Before adding an element you could test the dictionary, but I'm not sure what you'd do then. It may depend on what you want to use the sort for.

For my particular project each object would have a unique id, so your idea would work in this case. But I think I found the proper way of doing it:

Sub Sort(Extends objs() As myCustomClass)
  Dim x() As Integer

  For k As Integer = 0 To UBound(objs)
    If (objs(k) Is Nil) Then
      objs.Remove(k)
    Else
      x.Append(objs(k).SortByValue)
    End If
  Next

  x.SortWith(objs)

End Sub

Basically, you pick a value to sort the array by... in this code snip it is an Integer property called SortByValue. I suppose I could modify it so that it could take different sortBy values, such as by Timestamp, name, etc.

Notice that this is extending an array of myCustomClass.

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>


<Prev in Thread] Current Thread [Next in Thread>