对 NSString 的 NSArray 进行排序

发布于 2024-10-09 12:28:49 字数 384 浏览 0 评论 0原文

有人可以告诉我用于排序 NSMutableArray 的代码吗?我有以下 NSMutableArray: ,

NSMutableArray *arr = [[NSMutableArray alloc] init];

其中包含“2”、“4”、“5”、“1”、“9”等元素,这些元素都是 NSString。

我想按降序对列表进行排序,以便最大值的整数在列表中最高(索引 0)。

我尝试了以下操作:

[arr sortUsingSelector:@selector(compare:)];

但它似乎没有正确排序我的值。

有人可以向我展示正确完成我想要完成的任务的代码吗?谢谢!

Can someone please show me the code for sorting an NSMutableArray? I have the following NSMutableArray:

NSMutableArray *arr = [[NSMutableArray alloc] init];

with elements such as "2", "4", "5", "1", "9", etc which are all NSString.

I'd like to sort the list in descending order so that the largest valued integer is highest in the list (index 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

森林很绿却致人迷途 2024-10-16 12:28:49

您应该使用此方法:

[arr sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

NSArray 中或:

[arr sortUsingSelector:@selector(caseInsensitiveCompare:)];

在“就地”排序中 NSMutableArray

比较器应返回以下值之一:

  • NSOrderedAscending
  • <代码>NSOrderedDescending
  • NSOrderedSame

You should use this method:

[arr sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

in a NSArray or:

[arr sortUsingSelector:@selector(caseInsensitiveCompare:)];

in a "inplace" sorting NSMutableArray

The comparator should return one of this values:

  • NSOrderedAscending
  • NSOrderedDescending
  • NSOrderedSame
爱的十字路口 2024-10-16 12:28:49

编写自己的字符串比较方法非常简单:

@implementation NSString(compare)

-(NSComparisonResult)compareNumberStrings:(NSString *)str {
    NSNumber * me = [NSNumber numberWithInt:[self intValue]];
    NSNumber * you = [NSNumber numberWithInt:[str intValue]];

    return [you compare:me];
}

@end

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
流年里的时光 2024-10-16 12:28:49

如果数组的元素只是带有数字而没有字母的 NSString(即“8”、“25”、“3”等),那么这里有一个实际有效的干净而简短的方法:

NSArray *sortedArray = [unorderedArray sortedArrayUsingComparator:^(id a, id b) {
    return [a compare:b options:NSNumericSearch];
}];

完成!无需编写返回 NSComparisonResult 的整个方法,或八行 NSSortDescriptor...

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:

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...

锦上情书 2024-10-16 12:28:49

恕我直言,对此类数组进行排序的最简单方法是

[arr sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"self.intValue" ascending:YES]]]

如果您的数组包含浮点值,只需将键更改为 self.floatValue 或 self.doubleValue

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

潦草背影 2024-10-16 12:28:49

最简单的方法是创建一个像这样的比较器方法

NSArray *sortedStrings = [stringsArray sortedArrayUsingComparator:^NSComparisonResult(NSString *firstString, NSString *secondString) {
    return [[firstString lowercaseString] compare:[secondString lowercaseString]];
}];

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]];
}];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文