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

后端 未结 7 2074
星月不相逢
星月不相逢 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:06

    I tried the answer provided by ohhorob, but the objects were still being sorted alphabetically. Then I ran across this in another answer (https://stackoverflow.com/a/4550451/823356):

    I changed my NSSortDescriptor and it now sorts numerically.

    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"score"
                                                   ascending:YES
                                                  comparator:^(id obj1, id obj2) {
        return [obj1 compare:obj2 options:NSNumericSearch];
    }];
    

    I just thought I'd drop this in here as it solved my 'alphabetical sorting of NSNumbers' problem

提交回复
热议问题