objective-c - NSArray 使用的排序算法是否稳定?

标签 objective-c ios sorting nsarray

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/

相关文章:

objective-c - 如何在运行时检查类方法是否存在?

c++ - C++ 中二进制数据(长度不同)的字典顺序

ruby - <=> 和 - 在 sort 方法的 block 中有什么区别?

python - 多序列比对(最长公共(public)子序列)?

objective-c - Obj-C -> 递增数字(并在 Cocoa 标签上显示步骤)

objective-c - Main.1 输出文件在 XCode 中有何作用?

ios - fatal error : unexpectedly found nil while unwrapping an Optional value (Swift & Parse)

ios - 为collectionView添加deleteItemAtIndexPath

python - 根据嵌套键对字典列表进行排序

objective-c - Objective-C : Detecting Leakages via Instruments in Xcode