How Do I sort an NSMutable Array with NSNumbers in it?

后端 未结 7 1968
星月不相逢
星月不相逢 2020-12-05 02:06

I\'m trying to make a high score table, and suck at arrays in objective c (actually, in general objective c is challenging for me), so I can\'t figure out how to sort. I\'m

相关标签:
7条回答
  • 2020-12-05 02:32

    For all it's worth, here is another shorthand method:

    NSArray* n = @[@8, @3, @1, @12, @16, @7, @0, @5];
    
    NSArray* asc = [n sortedArrayUsingComparator:^NSComparisonResult(NSNumber* n1, NSNumber* n2) {
                return [n1 compare:n2];
            }];
    NSLog(@"Ascending: %@", asc);
    
    NSArray* dec = [n sortedArrayUsingComparator:^NSComparisonResult(NSNumber* n1, NSNumber* n2) {
               return [n2 compare:n1];
           }];
    NSLog(@"Descending: %@", dec);
    
    0 讨论(0)
提交回复
热议问题