NSArray使用的排序算法是否稳定排序? [英] Are the sorting algorithms used by NSArray stable sorts?

查看:132
本文介绍了NSArray使用的排序算法是否稳定排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSArray中各种排序方法使用的排序算法稳定? (因为它们是稳定排序算法,其中具有相同排序键的项目保留其相对顺序。)

Are the sorting algorithms used by the various sorting methods in NSArray stable? (As in are they "stable sort" algorithms, where items with the same sort key have their relative orders preserved.)

推荐答案

除非您使用 NSSortStable ,否则无法保证稳定排序。来自有关NSSortOptions的文档

Stable sort is not guaranteed unless you use NSSortStable. From the documentation on NSSortOptions:


NSSortStable

指定排序结果应该返回比较项目按其最初发生的顺序具有相同值。

Specifies that the sorted results should return compared items have equal value in the order they occurred originally.

如果此选项未指定,可能会或可能不会按原始顺序返回相同的对象。

如果您需要保证稳定排序,尝试类似:

If you need to guarantee a stable sort, try something like:

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

这篇关于NSArray使用的排序算法是否稳定排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆