如何在其中对包含Numbers的NSMutableArray进行排序?

时间:2010-07-08 16:22:50

标签: iphone objective-c arrays

我正在尝试制作一个高分表,并且在目标c中吮吸数组(实际上,一般来说,目标c对我来说很有挑战性),所以我无法弄清楚如何排序。我正在尝试做这样的事情(sp​​eudocode,我用动作风格写这个,因为我对它更熟悉):

highscores.addObjecttoArray(score)
highscores.sort(ascending)

但是我无法弄明白......我已经看到了其他关于它的线索,但他们使用plist文件和东西,我不知道足够的目标c来向他们学习。

7 个答案:

答案 0 :(得分:137)

你想这么做吗?

如果您有一组NSNumber实例的可变数组:

NSSortDescriptor *highestToLowest = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO];
[mutableArrayOfNumbers sortUsingDescriptors:[NSArray arrayWithObject:highestToLowest]];

美好而轻松:)

您还可以使用不可变数组上的描述符执行类似的排序,但最终会得到一个副本,而不是就地排序。

答案 1 :(得分:27)

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

如果它们肯定都是NSNumber,那么应该有效。

(添加对象是:

[highscores addObject:score];

如果要对降序排序(最高优先级):

10.6 / iOS 4:

[highscores sortUsingComparator:^(id obj1, id obj2) {
    if (obj1 > obj2)
        return NSOrderedAscending;
    else if (obj1 < obj2)
        return NSOrderedDescending;

    return NSOrderedSame;
}];

否则您可以定义类别方法,例如:

@interface NSNumber (CustomSorting)

- (NSComparisonResult)reverseCompare:(NSNumber *)otherNumber;

@end

@implementation NSMutableArray (CustomSorting)

- (NSComparisonResult)reverseCompare:(NSNumber *)otherNumber {
    return [otherNumber compare:self];
}

@end

并称之为:

[highscores sortUsingSelector:@selector(reverseCompare:)];

答案 2 :(得分:3)

我尝试了ohhorob提供的答案,但对象仍按字母顺序排序。然后我在另一个答案(https://stackoverflow.com/a/4550451/823356)中遇到了这个问题:

我改变了我的NSSortDescriptor,现在它按数字排序。

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

我只是认为我会把它放在这里,因为它解决了我对'NSNumbers'问题的字母排序

答案 3 :(得分:2)

对于它的全部价值,这是另一种简写方法:

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);

答案 4 :(得分:0)

此代码将按升序对NSNumber的NSMutableArray进行排序:

[tempRA sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    return [obj1 compare:obj2];
}];

此代码将按降序对NSNumber的NSMutableArray进行排序:

[tempRA sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    return [obj1 compare:obj2] * -1; // reverse the enum
}];

代码使用NSNumber compare:方法返回NSComparisonResult的事实,范围从-1到1.将compare的结果乘以-1会反转结果,从而将排序反转为降序。

答案 5 :(得分:0)

距离是一个关键,它的价值链接(浮动或双重)

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"distance"
ascending:YES
comparator:^(id obj1, id obj2)
                     {
                  return [obj1 compare:obj2 options:NSNumericSearch];
                     }];
NSArray *myArray = [NSArray arrayWithArray:arraySubCat];
myArray=[myArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor,nil]];
arraySubCat = [myArray copy];

答案 6 :(得分:-1)

降序订单:

NSArray *DescendingArray = [MinMaxArray sortedArrayUsingDescriptors:
                                @[[NSSortDescriptor sortDescriptorWithKey:@"doubleValue"
                                                                ascending:NO]]];

升序

NSArray *AscendingArray = [MinMaxArray sortedArrayUsingDescriptors:
                                @[[NSSortDescriptor sortDescriptorWithKey:@"doubleValue"
                                                                ascending:YES]]];
相关问题