Sort NSArray of NSDictionaries ignore case - ios

I am sorting an NSArray of NSDictionaries which is working using the following code
NSArray *getIndexArray = [nicknamesCombinedArray copy];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"MANUFACTURER" ascending:YES];
NSDictionary *sortedGetIndexArray = [getIndexArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
The only issue is that when it sorts if the word is all CAPS then it sorts like this
JESS
Jack
Jelly
Job
Where I would like it to be
Jack
Jelly
JESS
Job

Try this code..
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"MANUFACTURER" ascending:YES selector:#selector(localizedCaseInsensitiveCompare:)];

Related

How to sort array of dictionaries by date using sort descriptor

I have an array of dictionaries where dictionary is like this.
Rows = (
"<DriverRowRecord: 0x7f8de3a240d0>",
"<DriverRowRecord: 0x7f8de3a18790>"
);
Sections = "<DriverSectionRecord: 0x7f8de3a2c5a0>";
Here DriverRowRecord and DriverSectionRecord are separate classes. In DriverSectionRecord I have a date property.
#interface DriverSectionRecord : NSObject
#property(nonatomic,retain)NSDate *date;
#end
Now I want to sort the array based on DriverSectionRecord's date property. If the dictionary contains the date key I sort it like this.
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"date" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObject:descriptor];
NSArray *Ascendingorder = [Array sortedArrayUsingDescriptors:descriptors];
However, it doesn't work as date is a property of DriverSectionRecord. How can I achieve this?
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"Sections.date" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObject:descriptor];
NSArray *Ascendingorder = [Array sortedArrayUsingDescriptors:descriptors];
I got he solution from above comments. Thanks to all for such a great clarification.

"localizedStandardCompare" depends on the language preference of the iOS device

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES selector:#selector(localizedStandardCompare:)];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor,nil]];
I am working on an app that has a list of names, where all names are in Íslenska(Icelandic) language.
The above code works fine if the language selected for apps in settings is Íslenska(Icelandic) but the sort descriptor fails if the language is other than Íslenska(Icelandic).
Is there any way to resolve this dependency problem?
This should create a sortDescriptor that will compare strings using is_IS locale.
NSSortDescriptor* sortDescriptior =[[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES comparator:^(NSString* str1, NSString* str2) {
static NSStringCompareOptions comparisonOptions =
NSCaseInsensitiveSearch | NSNumericSearch |
NSWidthInsensitiveSearch | NSForcedOrderingSearch;
return [str1 compare:str2 options:comparisonOptions range:NSMakeRange(0, str1.length) locale:[NSLocale localeWithLocaleIdentifier:#"is_IS"]];
}];

Sort NSMutableArray of PFObjects by creationDate

How would I sort an array of PFObjects by their creationDate? I've tried something like the following, but this does not produce the desired result.
NSSortDescriptor *dateDescriptor = [NSSortDescriptor
sortDescriptorWithKey:#"creationDate"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:dateDescriptor];
NSArray *sortedEventArray = [nodeEventArray
sortedArrayUsingDescriptors:sortDescriptors];
NSSortDescriptor *dateDescriptor = [NSSortDescriptor
sortDescriptorWithKey:#"createdAt"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:dateDescriptor];
NSArray *sortedEventArray = [nodeEventArray
sortedArrayUsingDescriptors:sortDescriptors];
Or alternatively updatedAt for last updated.
Change dateDescriptor to:
NSSortDescriptor *dateDescriptor = [NSSortDescriptor
sortDescriptorWithKey:#"self.creationDate"
ascending:YES];
You need to add self if you want to check properties in objects inside the array.

NSSortDescriptor with two equally important keys

I am constructing a NSFetchedResultsController for my entity, which has two attributes, let's say a shortName and a longName. All of the managed objects only have either the shortName or the longName.
How can I sort the objects based on both attributes (or rather the one which is not null) at the same time?
Clearly the following will not work in my case:
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:#"shortName" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:#"longName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
It's quite interesting, but Im almost sure it is not possible to do. Just assigning shortName to the longName attribute, if there is no longName, while inserting to your db would be the best solution for me and simply sorting on longName.

setting multiple sort descriptors

I have a array of objects and need to sort it by rating and after by number of votes descendent(so that if two 5 stars rated elements compare, the one with the most votes should be first)
Is it possible to sort a NSArray by two descriptors:first after rating and then after votes count?
I've found on http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSSortDescriptor_Class/Reference/Reference.html
something like sortUsingDescriptors: but i can't find it anywhere in the docs, think it's deprecated.
Yes you can:
NSSortDescriptor *sortRating = [[NSSortDescriptor alloc] initWithKey:#"rating" ascending:NO];
NSSortDescriptor *sortVotes = [[NSSortDescriptor alloc] initWithKey:#"votes" ascending:NO];
NSArray *sortedArray = [orignalAray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortRating, sortVotes, nil]];
[sortRating release], sortRating = nil;
[sortVotes release], sortVotes = nil;
You are basically right. On NSArray there is the
- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors
method. This will return a new array, sorted according to various descriptors.
- (void)sortUsingDescriptors:(NSArray *)sortDescriptors
does exist, but is on NSMutableArray, which might explain why you couldn't find it in the documentation for NSArray. It accomplishes the same goal, but sorts the array you call it on, rather than returning a new array. Neither is deprecated.
Here's a one-liner.
NSArray *sortedArray = [unsortedArray sortUsingDescriptors:[NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey#"rating" ascending:NO], [NSSortDescriptor sortDescriptorWithKey#"date" ascending:NO], nil]];
Use - (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors. Here is the documentation: NSArray Class Reference
E.g:
NSSortDescriptor *sortRating = nil;
NSSortDescriptor *sortDate = nil;
NSSortDescriptor *sortRating = [[NSSortDescriptor alloc] initWithKey:#"rating" ascending:NO];
NSSortDescriptor *sortDate = [[NSSortDescriptor alloc] initWithKey:#"date" ascending:NO];
NSArray *sortedArray = [auxArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortRating,sortDate,nil]];
[sortRating release];
sortRating = nil;
[sortDate release];
sortDate = nil;
Cheers!
If you have one NSArray use this one:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"x_date" ascending:TRUE];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:#"x_time" ascending:TRUE];
[tempArray sortUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor,sortDescriptor1, nil]];

Resources