NSArray 排序不是整数格式

发布于 2024-12-11 12:38:12 字数 1299 浏览 0 评论 0原文

NSArray 排序时遇到问题

我在使用

NSArray *ratio = [NSArray arrayWithObjects:
                  @"10",@"14",@"23",@"21",@"24",@"26",@"26",@"28",@"29",@"0",
                  @"-11",@"-22",@"-33",@"-44",@"-55",@"-66",
                  @"-77",@"-88",@"-99",@"-12",@"12",nil];

NSSortDescriptor *sortOrder = [NSSortDescriptor sortDescriptorWithKey:@"self"  ascending: NO];
NSArray *sortedArray = [ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]];

NSLog(@"%@",[ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]]);
NSLog(@"Biggest : %@", [sortedArray objectAtIndex:0]);
NSLog(@"Smallest : %@", [sortedArray lastObject]);

但无法像整数一样排序:( 排序字符串格式如下所示; 9, 9, 9, 8, 7, 7, 6, 43, 4, 4, 31, 30, 29, 29, 28, 28, 27, 27)

我尝试了该代码但给出了错误:( NSArray *sortedArray = [ratiosortedArrayUsingComparator:^(NSString * str1, NSString * str2) {

    if ([str1 integerValue] < [str2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }

    if ([str1 integerValue] > [str2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
   return (NSComparisonResult)NSOrderedSame;
}];

错误说;正在初始化不兼容的块指针类型.....预期的 NSComparator

i have a problem at NSArray Sorting

i use

NSArray *ratio = [NSArray arrayWithObjects:
                  @"10",@"14",@"23",@"21",@"24",@"26",@"26",@"28",@"29",@"0",
                  @"-11",@"-22",@"-33",@"-44",@"-55",@"-66",
                  @"-77",@"-88",@"-99",@"-12",@"12",nil];

and i used

NSSortDescriptor *sortOrder = [NSSortDescriptor sortDescriptorWithKey:@"self"  ascending: NO];
NSArray *sortedArray = [ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]];

NSLog(@"%@",[ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]]);
NSLog(@"Biggest : %@", [sortedArray objectAtIndex:0]);
NSLog(@"Smallest : %@", [sortedArray lastObject]);

but can not sort like integer:( sorted string format looks like following ;
9, 9, 9, 8, 7, 7, 6, 43, 4, 4, 31, 30, 29, 29, 28, 28, 27, 27)

i tried that code but gives error :(
NSArray *sortedArray = [ratio sortedArrayUsingComparator:^(NSString * str1, NSString * str2) {

    if ([str1 integerValue] < [str2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }

    if ([str1 integerValue] > [str2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
   return (NSComparisonResult)NSOrderedSame;
}];

Error says; incompatible block pointer types initializing ..... expected NSComparator

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

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

发布评论

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

评论(2

一场春暖 2024-12-18 12:38:12

这很完美:

NSArray *sortedArray2 = [ratio sortedArrayUsingComparator:^(id str1, id str2) {

    if ([str1 integerValue] < [str2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }

    if ([str1 integerValue] > [str2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];

NSLog(@"%@",[ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]]);
NSLog(@"Biggest : %@", [sortedArray2 objectAtIndex:0]);
NSLog(@"Smallest : %@", [sortedArray2 lastObject]);

This works perfectly:

NSArray *sortedArray2 = [ratio sortedArrayUsingComparator:^(id str1, id str2) {

    if ([str1 integerValue] < [str2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }

    if ([str1 integerValue] > [str2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];

NSLog(@"%@",[ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]]);
NSLog(@"Biggest : %@", [sortedArray2 objectAtIndex:0]);
NSLog(@"Smallest : %@", [sortedArray2 lastObject]);
财迷小姐 2024-12-18 12:38:12

尝试使用 @"intValue" 作为键来代替排序描述符中的 @"self"

Try using @"intValue" as the key in place of @"self" in the sort descriptor.

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