NSArray中各种排序方式使用的排序算法是什么stable ? (因为它们是“稳定排序”算法,其中具有相同排序键的项目保留了它们的相对顺序。)
最佳答案
除非您使用 NSSortStable
,否则无法保证稳定排序。来自documentation on NSSortOptions :
NSSortStable
Specifies that the sorted results should return compared items have equal value in the order they occurred originally.
If this option is unspecified equal objects may, or may not, be returned in their original order.
如果你需要保证一个稳定的排序,尝试这样的事情:
[array sortWithOptions:NSSortStable usingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
关于objective-c - NSArray 使用的排序算法是否稳定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10486744/