The documentation for NSSortStable is non-grammatical - what is it trying to say?

I have an array that I would like to sort, and since Blocks are "black" this year, I looked

- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr


I looked at which choice to use and the documentation NSSortStable


Indicates that sorted results should return comparable items that have the same value in the order in which they originally occurred.

If this parameter is not specified, equal objects may or may not be returned in their original order.

I didn't have enough coffee to understand what it says, the first sentence is not even grammatically correct.

Can anyone translate to English for dummies?


source to share

3 answers


indicates that if two objects compare in the same way, their order should remain the same.

For example, consider the following:

NSMutableArray *array = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil];
[array sortWithOptions:0 usingComparator:^NSComparisonResult(id obj1, id obj2) {
    if ( [obj1 length] < [obj2 length] )
        return NSOrderedAscending;
    if ( [obj1 length] > [obj2 length] )
        return NSOrderedDescending;
    return NSOrderedSame;


If you don't specify NSSortStable

, the sorted array can be either (one, two, four, three)

or (two, one, four, three)

, since one

both two

are the same length. Both results are accepted. This allows the sorting algorithm to execute (slightly) faster.

When specified, NSSortStable

objects that compare the same way must be returned in original order (i.e. first one

, then two




A persistent sort is one that preserves the order of the elements as much as possible.

For example, if you are sorting last names, persistent sort will keep the names in the same order as they originally appeared in the container. That is, if we had:

Bob Smith
Tom Jones
Dave Smith
Fred Smith
Al Jones


He would sort

Tom Jones
Al Jones
Bob Smith
Dave Smith
Fred Smith


Note that Tom is still taller than Al and Bob is still taller than Dave, who is still taller than Fred.

An "unstable sort" will not try to preserve secondary ordering and may be slightly faster as a result.



I believe that "is" should be "have", for example:

It indicates that results should be returned sorted elements being compared with the same value in the order in which they originally occurred.

That is, if items of equal value are compared, those items must retain their sorting relative to each other in the final results.



All Articles