Re: Adding objects to NSMutableArray out of order?
Re: Adding objects to NSMutableArray out of order?
- Subject: Re: Adding objects to NSMutableArray out of order?
- From: Graham Cox <email@hidden>
- Date: Tue, 5 May 2009 11:18:59 +1000
On 05/05/2009, at 9:50 AM, Colin Cornaby wrote:
I'm dealing with some code that returns results out of order, but
the results contain the proper index. In the end, I'd like to put
them all into an NSArray, but unlike a C array, NSArray doesn't seem
to allow me to simply reserve X number of slots and fill the slots
as the results come in.
One workaround I can see is to actually store the results in a C
array, and then initialize the NSArray from the C array. I could
also write an algorithm that would try to rearrange the
NSMutableArray as results come back, but that seems more complicate.
Initializing from a C array looks like the best bet, but this is a
commonly called function of the software that is performance
sensitive, and could be dealing with large sets of data, so I was
wondering about the overhead of this approach.
Has anyone here run into a similar situation?
Simple. Just add the results to an NSMutableArray as they come in,
then when you have all the data, sort it using a sort function that
compares the index property of the object.
As long as you sort once, at the end, rather than for each item, it
should be fast.
Alternatively you could do an insertion sort for each item using a
binary search to locate the correct index. But this won't be any
faster overall, since you're just spreading the cost of the sort
across the number of items as they are received.
--Graham
_______________________________________________
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