使用包含数字的字符串对NSMutableArray进行排序? [英] Sort NSMutableArray with strings that contain numbers?

查看:88
本文介绍了使用包含数字的字符串对NSMutableArray进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSMutableArray,它向用户存储了高分.我想按数字排列项目(数字存储在NSStrings中.)
示例:
4,2,7,8 <到> <2> 4,4,7,8
是什么如果数据存储在NSStrings中,最简单的方法呢?

I have a NSMutableArray and it has the users high scores saved into it. I want to arrange the items numerically (the numbers are stored in NSStrings.)
Example:
4,2,7,8
To
2,4,7,8
What is the simplest way to do this if the data is stored in NSStrings?

推荐答案

此代码可以做到:

//creating mutable array
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@"4", @"2", @"7", @"8", nil];

//sorting
[myArray sortUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) {
    return [str1 compare:str2 options:(NSNumericSearch)];
}];

//logging
NSLog(@"%@", myArray);

它使用块,请确保您的目标OS支持该块(对于iOS是4.0,对于OSX是10.6).

It uses blocks, make sure your target OS supports that (It's 4.0 for iOS and 10.6 for OSX).

这篇关于使用包含数字的字符串对NSMutableArray进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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