On Sep 30, 2006, at 6:47 AM, Fred of Occam wrote:
The array of objects must itself be an object, or part of one.
Actually, I have it working without a wrapper object. I posted this
technique earlier, but I provide a little more detail here. See below:
Create a method of that object which performs a sort on the array,
using some sorting key.
Swap sort is the simplest, just comparing adjacent items and
swapping them if required.
It is of complexity N squared, so if your list is very long
something like quick sort might be better.
Both of these need little extra memory.
If the object has any events, one of these could be used to call
the sort when required.
More likely you will have to call the sort externally whenever you
read or write to the array of objects.
I could have used Charles SortLibrary or I could have tried to create
my own (and much slower) sorting algorithm. What I ended up doing
was taking advantage of the "new" SortWith() method (I think this was
introduced in REALbasic 2005r1).
The built-in Sort() and SortWith() only works with Double(), Integer
(), Single() and String(). If your object has one of these
properties, then you can implement a sort function based on its
value. In order to get the sort to work, I have loop through all of
the Objects and retrieve its value into a new temporary array. The
only real problem with this is how to handle Objects which are Nil;
and in my original technique I just ignored them. I decided this was
not the "correct" thing to do and will append Nil objects to the end
of the array after the sort so that the size of the array does not
change.
For this example, class Foo has three properties:
Class Foo Properties:
Name As String
ID As Integer
Percentage As Double
So to allow Foo() arrays to be sorted by all three properties, I have
to implement three different methods, and I have to put them either
in another class or in a module. I choose to put them into a Module
and use the Extends keyword to make it appear more like the built-in
Sort() and SortWith() methods.
Sub SortByName(Extends f() As Foo)
Dim v() As String
Dim k, nilCount As Integer
Do Until (k > UBound(f))
If (f(k) Is Nil) Then
f.Remove(k)
nilCount = nilCount + 1
Else
v.Append(f(k).Name) // get the property value here
k = k + 1
End If
Loop
v.SortWith(f) // here REALbasic does the sort for me
For k = 1 To nilCount // add back Nil objects
f.Append(Nil)
Next
End Sub
The other sort methods are a straight copy except the type for v()
and the property access. If you only want to provide a sort on a
single property, you can just name the method Sort().
_______________________________________________
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>