简体   繁体   English

如何使用NSNumbers对NSMutable数组进行排序?

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

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. 我正在尝试制作一个高分表,并且在目标c中吮吸数组(实际上,一般来说,目标c对我来说很有挑战性),所以我无法弄清楚如何排序。 I'm trying to do something like this (speudocode, I'm writing this in actionscript style because I'm more comfortable with it): 我正在尝试做这样的事情(sp​​eudocode,我用动作风格写这个,因为我对它更熟悉):

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

But I can't figure it out... I've seen other threads about it, but their use plist files and stuff and I don't know enough objective c to learn from them. 但我无法弄明白......我已经看到了其他关于它的线索,但他们使用plist文件和东西,我不知道足够的客观c来向他们学习。

Would you like to do that the short way? 你想这么做吗?

If you have a mutable array of NSNumber instances: 如果您有一个可变数组的NSNumber实例:

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

Nice and easy :) 好,易于 :)

You can also perform similar sorting with descriptors on immutable arrays, but you will end up with a copy, instead of in-place sorting. 您还可以使用不可变数组上的描述符执行类似的排序,但最终会得到一个副本,而不是就地排序。

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

Should work if they're definitely all NSNumber s. 如果他们肯定是所有的NSNumber都应该工作。

(Adding an object is: (添加对象是:

[highscores addObject:score];

)

If you want to sort descending (highest-first): 如果要按降序排序(最高优先级):

10.6/iOS 4: 10.6 / iOS 4:

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

    return NSOrderedSame;
}];

Otherwise you can define a category method, eg: 否则,您可以定义类别方法,例如:

@interface NSNumber (CustomSorting)

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

@end

@implementation NSMutableArray (CustomSorting)

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

@end

And call it: 并称之为:

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

I tried the answer provided by ohhorob, but the objects were still being sorted alphabetically. 我尝试了ohhorob提供的答案,但对象仍按字母顺序排序。 Then I ran across this in another answer ( https://stackoverflow.com/a/4550451/823356 ): 然后我在另一个答案( https://stackoverflow.com/a/4550451/823356 )中遇到了这个问题:

I changed my NSSortDescriptor and it now sorts numerically. 我改变了我的NSSortDescriptor,它现在按数字排序。

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 我只是想把它放在这里,因为它解决了我对“NSNumbers”按字母顺序排序的问题

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

This code will sort an NSMutableArray of NSNumber in ascending order: 此代码将按升序对NSNumber的NSMutableArray进行排序:

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

This code will sort an NSMutableArray of NSNumber in descending order: 此代码将按降序对NSNumber的NSMutableArray进行排序:

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

The code uses the fact that the NSNumber compare: method returns an NSComparisonResult that ranges from -1 to 1. Multiplying the result of the compare by -1 reverses the result and thereby reverses the sort to descending order. 该代码使用以下事实:NSNumber compare:方法返回一个范围从-1到1的NSComparisonResult。将compare的结果乘以-1会反转结果,从而将排序反转为降序。

distance is a key and it's value link (float or double) 距离是一个关键,它的价值链接(浮动或双重)

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

For Descending Order: 对于降序

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

For ascending Order 升序

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM