• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSMutableArray comparator; sorting on insert
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSMutableArray comparator; sorting on insert


  • Subject: Re: NSMutableArray comparator; sorting on insert
  • From: Dave DeLong <email@hidden>
  • Date: Fri, 16 Jan 2009 10:24:46 -0700

An alternative (though one that might be slightly less flexible than a regular NSArray) is to use a datastructure that automatically sorts on insert.

A guy in our Cocoaheads group has created the CHDatastructures framework (FOSS), which has all sorts of data structures not available in Cocoa, such as various trees (binary, AVL, RB, AA, Treaps, etc), stacks, queues, and dequeues. You can also have it construct an NSArray for you using various traversal orders, etc.

You can check it out of svn here:  http://cocoaheads.byu.edu/code/CHDataStructures

Cheers,

Dave DeLong

On Jan 16, 2009, at 10:18 AM, Greg Titus wrote:


On Jan 16, 2009, at 8:27 AM, David Harper wrote:

Hello,

I have written a comparator that returns an NSComparisonResult based on the comparison of two objects as required for

[(NSMutableArray *)someArray sortUsingSelector:@selector(theSelector:)]

Now, I want this array to remain sorted after each insert. For now I am inserting, then sorting, but this is not ideal. Is there a way to perform an insert using the same selector to find the correct index before inserting?

Bill Bumgarner asked good questions about whether you really need this or not, but if you do need it, another thing that you could do would be to go look at OmniFoundation <www.omnigroup.com/developer/ >, which includes a category on NSMutableArray for keeping arrays sorted:


// Maintaining sorted arrays
- (void)insertObject:(id)anObject inArraySortedUsingSelector: (SEL)selector;
- (void)insertObject:(id)anObject inArraySortedUsingFunction: (NSComparisonResult (*)(id, id, void *))compare context:(void *)context;
- (void)removeObjectIdenticalTo:(id)anObject fromArraySortedUsingSelector:(SEL)selector;
- (void)removeObjectIdenticalTo:(id)anObject fromArraySortedUsingFunction:(NSComparisonResult (*)(id, id, void *))compare context:(void *)context;


(These methods all use binary searches across the array contents.)

Hope this helps,
	Greg_______________________________________________

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

_______________________________________________

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


References: 
 >NSMutableArray comparator; sorting on insert (From: David Harper <email@hidden>)
 >Re: NSMutableArray comparator; sorting on insert (From: Greg Titus <email@hidden>)

  • Prev by Date: Re: NSMutableArray comparator; sorting on insert
  • Next by Date: Re: ObjC in time-critical parts of the code
  • Previous by thread: Re: NSMutableArray comparator; sorting on insert
  • Next by thread: Re: NSMutableArray comparator; sorting on insert
  • Index(es):
    • Date
    • Thread