Normally I make a wrapper class for the array with the following API
* AddItem(o As <yourclassname>)
* Protected AddItemOptimized(o As <yourclassname>)
* <yourclassname>() // constructor
* Item(Index As Integer) As <yourclassname>
* FindByYourCriteria(key As String, ByRef hadProblem As Boolean,
ByRef errMsg As String) As <yourclassname>
* Load() // in case you want to load the array from a file or database.
* IsDirty() // Check if any item changed
* Save() // again, in case this interfaces to a file or database
* ~<yourclassname>() // destructor to clean up
and I give my 'item' class (the one labeled <yourclassname>) a
'parent' property of type reference-to-wrapper-class which AddItem()
sets, as the items are added to the 'collection' (wrapper class).
This way the items in the collection can tell their container
class that they've been modified (the destructor calls the Save()
method before cleaning up memory in case the client code didn't call
Save() itself.); this will also result in the call to IsDirty() to
return True.
I usually do the sorting (insertion sort based on your key needs)
as the items are added. Note that the Load method calls
Me.AddItemOptimized() as it parses the data file/database record/
etc... Because the items are saved back to the file/database in
sorted order, the 'AddItemOptimized()' method is called by Load(),
and does the insertion sort 'in reverse' if you will (i.e. looks
backwards through the collection to find the insertion location.
Since the highest items are at the end, this results in usually only
having to do one comparison, which is better than the O(N)
performance the algorithms' books give!)
If my container is going to be read-only (the items within do not
need to - or cannot - be modified), then I eliminate the
AddItemOptimized(), IsDirty(), and Save() methods, and the Load()
method will set up the contents, calling the AddItem() method instead.
The FindByYourCriteria() method, does a binary search on the
collection for speed, since we know the items are already sorted.
Of course, the downside is that you take a O(N) performance hit
every time you call AddItem()! (which is why I do it all 'up front'
when I load the items from disk, and this technique is used whenever
I expect few insertions/deletions from the collection in "normal" use.)
On Sep 29, 2006, at 6:41 PM, Phil M wrote:
Is is possible to add a Sort function to an Object array? Can I
get sorts to work if the Object supports something like a
"Sortable" interface? For example:
Dim m() As myCustomClass
// add various items
m.Sort()
If the standard sort function is not available, can specify a
property to sort the objects by?
_______________________________________________
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>
_______________________________________________
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>