排序数组的第一个字符串比数字

时间:2014-01-31 09:08:25

标签: ios iphone sorting

我想排序包含字符串的数组以及A,B,1 A,2 A,V,Z,10等数字的数组想要以这种方式排序A,B,V,Z,1 A,2 A,10 B.我已经搜索了堆栈溢出但没有找到任何有用的东西。

请帮忙......

8 个答案:

答案 0 :(得分:1)

您可以使用比较器块根据类型对数组中的对象进行排序。类似的东西:

NSArray *array = @[ @"A", @"B", @1, @2, @"V", @"Z", @10 ];
NSArray *result = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {

    // If the objects are the same class, compare them normally
    if ([[obj1 class] isEqual:[obj2 class]]) {
        return (NSComparisonResult)[obj1 compare:obj2];
    }

    // If the objects are different classes, numbers go after strings 
    if ([obj1 isKindOfClass:[NSNumber class]] && [obj2 isKindOfClass:[NSString class]]) {
        return (NSComparisonResult)NSOrderedDescending;
    }

    if ([obj2 isKindOfClass:[NSNumber class]] && [obj1 isKindOfClass:[NSString class]]) {
        return (NSComparisonResult)NSOrderedAscending;
    }

return NSOrderedSame;
}];

NSLog(@"Result: %@", result);

答案 1 :(得分:0)

您可以使用比较器排序并检查nsnumber或nsstring

的实例

来自Apple Docs:

NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {

    // check for instance here f.e. [obj1 isKindOfClass:[NSString class]]

    if ([obj1 integerValue] > [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }

    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];

答案 2 :(得分:0)

尝试以下代码:

NSArray * arr =[[NSArray alloc]initWithObjects:@"01 Track1", @"02 Track2", @"33 Track3", @"Track1", @"Track2", @"Track3", @"07 ", nil];

NSArray *sortedTrack = [arr sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2)
{
    return [obj1 compare:obj2];
}];

NSLog(@"%@",sortedTrack);

输出:(     “01 Track1”,     “02 Track2”,     “07”,     “33 Track3”,     TRACK1,     磁轨2,     TRACK3 )

答案 3 :(得分:0)

我已经完成了测试。它工作正常。用这个。关于效率的怀疑。

 NSArray *sortedArray = [yourArray sortedArrayUsingComparator: ^(id obj1, id obj2) {

        if ([obj1 isKindOfClass:[NSString class]] && [obj2 isKindOfClass:[NSString class]] )
        {
            return [obj1 compare:obj2 options:NSCaseInsensitiveSearch];
        }
        else if ([obj1 isKindOfClass:[NSNumber class]] && [obj2 isKindOfClass:[NSNumber class]])
        {
            if ([obj1 integerValue] > [obj2 integerValue]) {
                return (NSComparisonResult)NSOrderedDescending;
            }

            if ([obj1 integerValue] < [obj2 integerValue]) {
                return (NSComparisonResult)NSOrderedAscending;
            }
        }
        else
        {
            if ([obj1 isKindOfClass:[NSNumber class]])
                return (NSComparisonResult)NSOrderedDescending;
            if ([obj2 isKindOfClass:[NSNumber class]])
                return (NSComparisonResult)NSOrderedAscending;
        }

        return (NSComparisonResult)NSOrderedSame;
    }];

答案 4 :(得分:0)

您可以使用NSPredicate进行过滤。查看documentation

NSArray *array = @[@"A", @1, @"Z", @"B", @10, @100, @"X"];

NSPredicate *numbersPredicate = [NSPredicate predicateWithFormat:@"self isKindOfClass: %@", [NSNumber class]];
NSPredicate *stringsPredicate = [NSPredicate predicateWithFormat:@"self isKindOfClass: %@", [NSString class]];

NSArray *numbersArray = [[array filteredArrayUsingPredicate:numbersPredicate] sortedArrayUsingSelector:@selector(compare:)];
NSArray *stringsArray = [[array filteredArrayUsingPredicate:stringsPredicate] sortedArrayUsingSelector:@selector(compare:)];

NSArray *sortedArray = [stringsArray arrayByAddingObjectsFromArray:numbersArray];

NSLog(@"sortedArray = %@", sortedArray);

结果;

sortedNumbers = (A, B, X, Z, 1, 10, 100)

答案 5 :(得分:0)

这是一个非常简短的版本,利用了Objective-C的动态绑定以及NSNumberNSString都响应doubleValuedescription的事实:

NSArray *array = @[ @"A", @"B", @1, @2, @"V", @"Z", @10 ];

NSArray *result = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {
    NSComparisonResult result = [@([obj1 doubleValue]) compare:@([obj2 doubleValue])];
    if (result != NSOrderedSame)
        return result;
    return [[obj1 description] compare:[obj2 description]];
}];

首先,我们将NSNumberNSString转换为双倍,将它们装箱并进行比较。非数字字符串将始终返回0,因此它们相等。

因此,如果数值比较相等,我们只需进行正常的字符串比较,确保两个操作数都是字符串。

(这会将负数放在非数字元素之前。)

答案 6 :(得分:0)

尝试以下代码:

NSArray *arr = @[@"A",@"B",@"Y",@"4 B",@"1 A",@"2 A",@"V",@"Z",@"10 B"];

NSArray *sortedArray;
sortedArray = [arr sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    int aa = [a intValue];
    int bb = [b intValue];

    if (aa !=0 && bb != 0)
    {
        return (aa > bb);
    }
    if (aa == 0 && bb == 0)
    {
        return [a compare:b];
    }
    if (aa == 0) return -1;
    return 1;

}];

输出:(A,B,V,Y,Z,1 A,2 A,4 B,10 B)

答案 7 :(得分:-2)

NSArray *value_array = @[ @"A", @"B", @"1", @"2", @"V",@"Z",@"10"];
NSArray *result_array = [value_array sortedArrayUsingDescriptors:
                    @[[NSSortDescriptor sortDescriptorWithKey:@"doubleValue"
                                                    ascending:YES]]];

NSLog(@"Sorted Array: %@", result_array);