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

I am trying to make a table with a high rating and suck the arrays in the c lens (in fact, in general, the goal of c is difficult for me), so I can not figure out how to sort. I am trying to do something like this (speudocode, I am writing this in ActionScript style because I am more comfortable):

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

But I canโ€™t understand it ... I have seen other topics about it, but they are used by plist files and stuff, and I donโ€™t know objective c enough to learn from them.

+45
arrays objective-c iphone
Jul 08 '10 at 16:22
source share
7 answers

Do you want to do this for a short time?

If you have a mutable array of NSNumber instances:

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

Nice and easy :)

You can also do similar sorting with descriptors on immutable arrays, but you end up with a copy instead of sorting in place.

+132
Jul 08 '10 at 20:59
source share

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

Should work if they are definitely all NSNumber s.

(Adding an object:

[highscores addObject:score];

)

If you want to sort downward (highest):

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, for example:

 @interface NSNumber (CustomSorting) - (NSComparisonResult)reverseCompare:(NSNumber *)otherNumber; @end @implementation NSMutableArray (CustomSorting) - (NSComparisonResult)reverseCompare:(NSNumber *)otherNumber { return [otherNumber compare:self]; } @end 

And call him:

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

+26
Jul 08 2018-10-10T00:
source share

I tried the answer provided by ohhorob, but the objects were still sorted alphabetically. Then I looked at this in another answer ( https://stackoverflow.com/a/165269/ ):

I changed my NSSortDescriptor and now it is sorted numerically.

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

I just thought I would drop it here because he solved my "alphabetically sorting NSNumbers problem"

+3
Jul 27 '12 at 15:05
source share

distance is the key and its value (floating 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]; 
+1
Apr 22 '16 at 8:37
source share

Thereโ€™s another abbreviated method for all this:

 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); 
+1
Nov 08 '16 at 14:27
source share

This code will sort NSMutableArray NSNumber in ascending order:

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

This code will sort NSMutableArray from NSNumber in descending order:

 [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 NSComparisonResult, which varies from -1 to 1. Multiplying the comparison result by -1 cancels the result and thereby cancels sorting in descending order.

0
May 20 '14 at 21:43
source share

For Descending Order:

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

In ascending order

 NSArray *AscendingArray = [MinMaxArray sortedArrayUsingDescriptors: @[[NSSortDescriptor sortDescriptorWithKey:@"doubleValue" ascending:YES]]]; 
-one
Feb 05 '15 at 9:11
source share



All Articles