如何在目标C中对NSMutableArray进行排序 [英] How to sort NSMutableArray in objective C

查看:67
本文介绍了如何在目标C中对NSMutableArray进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用计时器应用程序。
我有一个数组。我想按顺序排序数组。
假设我在数组中有5个元素,如10,100,50,30,70。

i am workinng on timer app. i have a an array.i want to sort array in accending order. suppose i have 5 element in array like 10,100,50,30,70.

我想要数组如10,30,50,70,100。

I want array like 10,30,50,70,100.

推荐答案

NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(id firstObject, id secondObject) {
    return [((NSString *)firstObject) compare:((NSString *)secondObject) options:NSNumericSearch];
}];

编辑:
如果元素是数字,请更改返回的类型和类型firstObject和secondObject。

If the elements are numbers, change the returned type and the type of the firstObject and secondObject.

这篇关于如何在目标C中对NSMutableArray进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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