I am having a problem regarding sorting an array w.r.t database:
NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@\"w\" ascending:YES];
NSAr
An alternative form of Apple's finder sort with locale method uses the comparator block, helpful if you're in an ARC environment and don't want to deal with bridging casts, etc:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"your_string_key" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch | NSWidthInsensitiveSearch | NSForcedOrderingSearch;
NSRange string1Range = NSMakeRange(0, ((NSString *)obj1).length);
return [(NSString *)obj1 compare: (NSString *)obj2 options: comparisonOptions range: string1Range locale: [NSLocale currentLocale]];
}];
NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:@[sortDescriptor]];
I too would recommend storing the current locale in a local variable for efficiency purposes.
This code is working fine for me.
- (void)sortSearchResultWithInDocumentTypeArray:(NSMutableArray *)aResultArray basedOn:(NSString *)aSearchString {
NSSortDescriptor * frequencyDescriptor =[[NSSortDescriptor alloc] initWithKey:aSearchString ascending:YES comparator:^(id firstDocumentName, id secondDocumentName) {
static NSStringCompareOptions comparisonOptions =
NSCaseInsensitiveSearch | NSNumericSearch |
NSWidthInsensitiveSearch | NSForcedOrderingSearch;
return [firstDocumentName compare:secondDocumentName options:comparisonOptions];
}];
NSArray * descriptors = [NSArray arrayWithObjects:frequencyDescriptor, nil];
[aResultArray sortUsingDescriptors:descriptors];
}
May I suggest using -localizedStandardCompare: (NSString)?
"This method should be used whenever file names or other strings are presented in lists and tables where Finder-like sorting is appropriate. The exact sorting behavior of this method is different under different locales and may be changed in future releases."
I think this will do the trick for you. The docs for it are here: String Programming Guide
Add this little function written by Apple.
int finderSortWithLocale(id string1, id string2, void *locale)
{
static NSStringCompareOptions comparisonOptions =
NSCaseInsensitiveSearch | NSNumericSearch |
NSWidthInsensitiveSearch | NSForcedOrderingSearch;
NSRange string1Range = NSMakeRange(0, [string1 length]);
return [string1 compare:string2
options:comparisonOptions
range:string1Range
locale:(NSLocale *)locale];
}
Make sure that you copy the function definition into your header, or you'll get a compile error on your sorted array.
For your sorted array, use this method:
[mGlossaryArray sortedArrayUsingFunction:finderSortWithLocale context:[NSLocale currentLocale]];
Your results will look like this:
You may use this for sorting an array according to name thats also contains small letter:
NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:@"w" ascending:YES selector:@selector(caseInsensitiveCompare:)];
NSArray *sortDescriptors = [NSArray arrayWithObject:sorter];
[mGlossaryArray sortUsingDescriptors:sortDescriptors];
This code work fine for me to sort name according to alphabets that has also small character i.e. rocky,Ajay,john,Bob etc.
Take a look here: Creating and Using Sort Descriptors
You can compare as case-insensitive.
NSSortDescriptor *sorter = [[[NSSortDescriptor alloc]
initWithKey:@"w"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject: sorter];
[mGlossaryArray sortUsingDescriptors:sortDescriptors];