Take the 2-minute tour ×

I have a NSMUtableArray which has elements, for example:


And I want to add an object d to behind c and before e. In other words, I'd like to insert an object to a sorted array.(The object can be a custom object, too)

I'd like to know : besides using for to find the position, is there any other method to implement it? It is better to use the iOS api.


share|improve this question
Bavarious's should be the right answer although the method is available in iOS4. For the earlier iOS, it may need to enumerate it yourself. Or add the object and sort the array, and then you can get the index. –  scorpiozj Nov 18 '11 at 13:25
add comment

3 Answers

up vote 51 down vote accepted

You can use -[NSArray indexOfObject:inSortedRange:options:usingComparator:] to ask an NSArray for the index where an object should be inserted given an array range that’s currently sorted.

For example, assuming the entire array is sorted::

NSMutableArray *array = …;
id newObject = …;
NSComparator comparator = …;

NSUInteger newIndex = [array indexOfObject:newObject
                             inSortedRange:(NSRange){0, [array count]}

[array insertObject:newObject atIndex:newIndex];

Since this method uses binary search, it is more efficient than iterating over all elements in the array.

The comparator is a block object that receives two objects of type id and returns an NSComparisonResult value.

share|improve this answer
thanks, it seems right!! I'll test it. –  scorpiozj Nov 18 '11 at 13:10
it works but it is available in iOS4 and later. –  scorpiozj Nov 18 '11 at 13:22
I am having the same problem and I am confused about the comparator.Please help me with that –  Sekhar Apr 19 '13 at 11:16
If anyone don't know how to use NSComparator -- NSComparator compareStuff = ^(id obj1, id obj2) { return NSOrderedSame; }; –  Seema Sharma Jan 31 at 6:54
Many object have compare: method, use it or write custom compare method for custom class –  AlKozin 2 days ago
add comment

To inject element to known index (position) use



And to find position of object previously placed into NSMutableArray use

- (int)indexOfObject:(id)anObject

NSMutableArray - Get Arrays Index Integer By Searching With A String

Section Finding Objects in an Array

share|improve this answer
thanks, but it is not finding the index of an element in a array. I'd like to insert an element in a sorted array. –  scorpiozj Nov 18 '11 at 13:09
add comment

I'd just add the new object at either end and sort the array again. If the array you're adding to is already sorted, the re-sort that moves one object is going to be about as quick as anything you'd implement yourself.

NSMutableArray *things; // populated 
id newObject;
[things addObject:newObject atIndex:0];
[things sortUsingSelector:@selector(compare:)];
share|improve this answer
thanks! In fact the array is data source of a uitableview. I need to know the index of the added object so that I can insert a new row. –  scorpiozj Nov 18 '11 at 13:28
This will work, of course, but the key question is how frequently do you plan to do this? The "add at end and re-sort" approach is lousy computationally compared to the "search and insert" approach. If the data set is large OR you do this frequently, you might consider the answer provided by @Bavarious above. –  Cubs Fan Ron Jan 30 '13 at 2:51
add comment

Your Answer


