I can't piece together how to do this.
I fetch my array from a plist, this array is full of numbers (as set in the plist). Now al I need to do is sort them so they are descending, but I can't work it out.
I can't piece together how to do this.
I fetch my array from a plist, this array is full of numbers (as set in the plist). Now al I need to do is sort them so they are descending, but I can't work it out.
Try this code?
NSArray *array = /* loaded from file */;
array = [array sortedArrayUsingSelector: @selector(compare:)];
-compare:
method is defined for NSNumber
instances: developer.apple.com/library/mac/documentation/Cocoa/Reference/…: –
Blum NSArray
called sortedArray
which calls exactly this? I love extensions and all, but why do I (and everyone else) have to write such basic stuff that Apple should have had in the standard libraries all along? Although Obj-C is a flawed language and Swift can't come quickly enough, it seems to me a bigger problem is the lousy libraries. I hope Swift's libraries don't leave me having to write out this basic stuff myself. –
Anaerobic compare:
. If the class you've inserted into your NSArray
doesn't implement that method, then you shouldn't be using the sortedArray
method. You'll have to fall back to the more flexible sortedArrayUsingSelector:
method. –
Anaerobic The following will sort the numbers in ascending order and then reverse the result to give the numbers in descending order:
NSArray *sorted = [[[array sortedArrayUsingSelector:@selector(compare:)] reverseObjectEnumerator] allObjects];
This previous question has some other alternatives: Sort an NSArray in Descending Order
-[__NSCFString sortedArrayUsingSelector:]: unrecognized selector sent to instance
. –
Gronseth sortedArrayUsingSelector:
on an NSArray
? You appear to be calling sortedArrayUsingSelector:
on an NSString
. You should probably check the data structure that is being loaded from the Plist. –
Sachasachem array
pointer points to NSString
object. Forgot to retain / too much [auto]releases? –
Occasional Here is one of many methods using comparison block. This code snippet is handy for any array with numbers that you want to sort. For Ascending order:
AscendingArray = [UnsortArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
For Descending order:
DescendingArray = [UnsortArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
compare:
? –
Hygrometry It work for me:
NSSortDescriptor *sortIdClient =
[NSSortDescriptor sortDescriptorWithKey:@"campaignValue"
ascending:NO
comparator: ^(id obj1, id obj2){
return [obj1 compare:obj2 options:NSNumericSearch];
}];
NSArray *sortDescriptors = @[sortIdClient];
NSArray *arrTemp = [self.allCampaignsList sortedArrayUsingDescriptors:sortDescriptors];
This Will solve the problem:
NSArray *array = /* loaded from file */;
array = [array sortedArrayUsingSelector: @selector(compare:)];
© 2022 - 2024 — McMap. All rights reserved.