排序数字目标C [英] Sorting Numbers Objective C

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

问题描述

我有一个字符串数组,可以使用方法进行排序

I have got an array of strings which i sort using the method

[TableViewPopoverList sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

这些字符串的第一部分是数字,但是例如,如果string1 ="16 Ag ....."和string2 ="8 Ag ....",则将其排序为string1,然后按string2排序,显然1,然后将其放在8之前.我要执行的操作是先看到它的全部数字,然后对string2进行排序,然后对string1进行排序.

The first part of these strings is a number, however for example if string1 = "16 Ag....." and string2 = "8 Ag...." it sorts it as string1 then string2, obviously it sees the 1 and puts it before the 8, What i want to do is have it so it sees the entire number so it sorts string2 then string1.

最简单的方法是什么.

谢谢

推荐答案

以这种方式执行...

Do it this way...

[TableViewPopoverList sortUsingComparator:^(NSString *obj1, NSString *obj2) {
    return [obj1 compare:obj2 options:(NSCaseInsensitiveSearch | NSNumericSearch)];
}];

这将在NSString上使用compare:options:方法,并且NSNumericSearch选项会将数字作为实际数字进行比较.

This uses the compare:options: method on NSString and the NSNumericSearch option will compare numbers as actual numbers.

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

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