Re: NSMutableArray sorting
Re: NSMutableArray sorting
- Subject: Re: NSMutableArray sorting
- From: John Nairn <email@hidden>
- Date: Sun, 21 Dec 2008 19:08:38 -0800
On Dec 21, 2008, at 9:20 AM, Scott Ribe wrote:
1. Building a c++ array of integers with indices to the new sort
order
2. Precalculate all crucial sort terms (that depend on external data)
for the objects to be sorted.
3. Sort the index array manually (e.g., a shell sort from Numerical
Recipes in C, pg 244 (my edition))
4. Once done, build a new array based on the sorted indices, and free
any allocated memory
An intermediate step would be to add a sortOrder int attribute to
the class,
then when the comparison selector is called, first calculate it if
it's not
already set, then use it.
I've done something similar; the sortOrder attribute was actually a
string,
but one whose calculation was somewhat expensive--caching the result
dramatically sped up the sort.
Yes, I think the key is that the expensive calculation is done only
once for each object rather than every time a comparison needs to be
made. After the caching, my approach does all the searching in one
method using swaps of integers. I don't know how much faster (in any)
that would be compared to internal NSArray sort code. It sorts about
4000 names (on a slow PowerBoook G4) in half second or less. Since a
common use of sorting is for data in columns of a table and clicking
twice just reverses the list, I added a custom option to check if the
list is sorted but backwards and then just swap elements in place if
it is.
If you are interested, I revised my code to be an NSMutableArray
category with a sort method that gets called from a "delegate." The
sort method calls the delegate to cache the objects and then later to
provide comparison results between those cache objects. You can get
it here:
http://homepage.mac.com/nairn/.Public/sorting.zip
---------------
John Nairn
GEDitCOM - Genealogy Software for the Macintosh
http://www.geditcom.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden