ios 对 NSString 的 NSArray 进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4558761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting an NSArray of NSString
提问by CodeGuy
Can someone please show me the code for sorting an NSMutableArray? I have the following NSMutableArray:
有人可以告诉我排序 NSMutableArray 的代码吗?我有以下 NSMutableArray:
NSMutableArray *arr = [[NSMutableArray alloc] init];
with elements such as "2", "4", "5", "1", "9", etc which are all NSString.
带有诸如“2”、“4”、“5”、“1”、“9”等元素,它们都是 NSString。
I'd like to sort the list in descending order so that the largest valued integer is highest in the list (index 0).
我想按降序对列表进行排序,以便最大的整数在列表中最高(索引 0)。
I tried the following:
我尝试了以下方法:
[arr sortUsingSelector:@selector(compare:)];
but it did not seem to sort my values properly.
但它似乎没有正确排序我的价值观。
Can someone show me code for properly doing what I am trying to accomplish? Thanks!
有人可以向我展示正确完成我想要完成的工作的代码吗?谢谢!
采纳答案by thelaws
It's pretty simple to write your own comparison method for strings:
为字符串编写自己的比较方法非常简单:
@implementation NSString(compare)
-(NSComparisonResult)compareNumberStrings:(NSString *)str {
NSNumber * me = [NSNumber numberWithInt:[self intValue]];
NSNumber * you = [NSNumber numberWithInt:[str intValue]];
return [you compare:me];
}
@end
回答by HyLian
You should use this method:
你应该使用这个方法:
[arr sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
in a NSArray
or:
在一个NSArray
或:
[arr sortUsingSelector:@selector(caseInsensitiveCompare:)];
in a "inplace" sorting NSMutableArray
在“就地”排序中 NSMutableArray
The comparator should return one of this values:
比较器应返回以下值之一:
NSOrderedAscending
NSOrderedDescending
NSOrderedSame
NSOrderedAscending
NSOrderedDescending
NSOrderedSame
回答by Eric
If the array's elements are just NSStrings with digits and no letters (i.e. "8", "25", "3", etc.), here's a clean and short way that actually works:
如果数组的元素只是带有数字且没有字母的 NSStrings(即“8”、“25”、“3”等),那么这里有一个简洁而有效的方法:
NSArray *sortedArray = [unorderedArray sortedArrayUsingComparator:^(id a, id b) {
return [a compare:b options:NSNumericSearch];
}];
Done! No need to write a whole method that returns NSComparisonResult
, or eight lines of NSSortDescriptor
...
完毕!无需编写返回的整个方法NSComparisonResult
,或八行NSSortDescriptor
...
回答by ReDetection
IMHO, easiest way to sort such array is
恕我直言,对此类数组进行排序的最简单方法是
[arr sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"self.intValue" ascending:YES]]]
If your array contains float values, just change key to self.floatValue
or self.doubleValue
如果您的数组包含浮点值,只是改变键self.floatValue
或self.doubleValue
回答by albertosh
The easiest way would be to make a comparator method like this one
最简单的方法是制作一个像这样的比较器方法
NSArray *sortedStrings = [stringsArray sortedArrayUsingComparator:^NSComparisonResult(NSString *firstString, NSString *secondString) {
return [[firstString lowercaseString] compare:[secondString lowercaseString]];
}];