finneycanhelp finneycanhelp - 1 month ago 6
Swift Question

Swift Stable Sort?

At this time (March 28, 2015): If I want to use a stable sort in swift, I have to use

NSArray
and
sortWithOptions
or write my own stable sort in Swift such as an insertion sort? I see in the Apple Swift docs that
sorted
is not stable.

Example of stable sort in NSMutableArray:

[array sortWithOptions:NSSortStable usingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];


Am I missing another option that's available to me in Swift other than using Objective-C or writing my own sort?

Answer

As @Michael put it, one can "write a swift-wrapper that calls the objective-c function"