I had a similar problem and there were serious performance issues with the built-in sorting. I could make it work with sortUsingFunction:context:, but the performance depends significantly on the efficiency of that method (i.e., how much work it takes to interact with that external data). In my case, when used for sorting a column with a few thousand lines, it got very slow. I was able to get more much faster sorting (at least 100x) by not relying on built- in tools.

If you are interested, here is the process I used:

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

Here is partial code. You would have to fill in the objects array and the method (BOOL)checkObj1:(id)obje1 vsObj2:(id)obj2 simply returns YES if the objects need rearranging (i.e., currently in the wrong order).

        // sorting objects in array order
        int *ind=(int *)malloc(([order count]+1)*sizeof(int));
NSMutableArray *objects=[[NSMutableArray alloc] initWithCapacity: [order count]];
        for(i=0;i<[order count];i++)
        {       ind[i+1]=i;
// fill index i of objects with information needed to sort object at item i in order
        }
        int n2,n=[order count];
        int lognb2=0,j,m,nn,ii,ij;
        n2=n>>1;
        while(n2>0)
        {       lognb2++;
                n2>>=1;
        }
        m=n;
        for(nn=1;nn<=lognb2;nn++)
        {       m>>=1;
                for(j=m+1;j<=n;j++)
                {       ii=j-m;
                        ij=ind[j];
while(ii>=1 && [self checkObj1:[objects objectAtIndex:ind[ii]] vsObj2:[objects objectAtIndex:ij]])
                        {       ind[ii+m]=ind[ii];
                                ii-=m;
                        }
                        ind[ii+m]=ij;
                }
        }
// build new array from sorted indices in ind[], i.e. index i of new array is index ind[i] from original array


It will all depend on how efficient you can make the function in the built-in sorting function. For me, the difference was dramatic.

See -sortUsingFunction:context: in NSMutableArray’s documentation. As
Ricky Sharp has already mentioned, there’s also -sortUsingSelector:,
however if you’re comparing partly with external data you might like
having the void * context provided by this method.

Sincerely,
Rob

On 20-Dec-08, at 7:15 AM, Jacob Rhoden wrote:

How do you sort an NSMutableArray, when the difference between the
two objects is determined by information not contained completely
within the objects themselves. ie in Java you can implement a
Comparator that takes two objects to compare then. Is this similar
in Cocoa?

ie in this case I need to apply a mathematical algorithm using
information from each object to determine which one is greater than
the other.

Thanks!
Jacob

---------------
John Nairn
GEDitCOM - Genealogy Software for the Macintosh
http://www.geditcom.com


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to