Probably the easiest thing to do, if you want an array is to create a category on NSMutableArray with a method that inserts an object in the right place. This is a bit unsatisfactory, however (there's nothing to stop you from inserting objects using the normal method and breaking the ordering), so you probably want your own collection class. You can create this easily by wrapping an NSMutableArray and a comparator in a new class. e.g.
@interface MyOrderedArray : NSObject <NSFastEnumeration>
-(id) initWithComparator: (NSComparator) anOrdering;
-(void) insertObject: (id) aNewObject;
// methods to access objects from the array
@end
@implementation MyOrderedArray
{
NSComaparator theOrdering;
NSMutableArray* backingArray;
}
-(id) initWithComparator: (NSComparator) anOrdering
{
self = [super init];
if (self != nil)
{
theOrdering = [anOrdering copy];
backingArray = [[NSMUtableArray alloc] init];
}
-(id) insertObject: (id) newObject
{
// use a binary search to find the index and insert the object there
}
Other methods can be passed to the backing array to implement e.g.
-(NSUInteger) count
{
return [backingArray count];
}
and this one will be useful:
- (NSUInteger)countByEnumeratingWithState: (NSFastEnumerationState*) state
objects: (id*) stackbuf
count: (NSUInteger) len
{
return [backingArray countByEnumeratingWithState: state
objects: stackbuf
count: len];
}