I want to sort an array using NSSortDescriptor

I had a problem sorting the wrt array:

NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"w" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObject: sorter]; [mGlossaryArray sortUsingDescriptors:sortDescriptors]; [sorter release]; 

Here, the database has the first few capital letters, and because of this capital letter, it does not show me the correct sorted output. Here I am sorting an array with rt "w", which is my table column in the database. Here I am attaching a screenshot for the output, which says that “Cancer” comes first than “c”, but this is not true, it does not give out alphabetically sorting due to headwords.

eg. if there is “capable” in lower case and “aCid”, then he will first show aCid and then he can, and there is also the case when the first letter is a cap, it occurs, for example, “Able” and “a”. Able is displayed here first. enter image description here

+69
sorting iphone jquery-ui-sortable nsarray
Apr 04 2018-11-18T00:
source share
7 answers

Take a look here: Creating and Using Sort Descriptors

You can compare case insensitivity.

 NSSortDescriptor *sorter = [[[NSSortDescriptor alloc] initWithKey:@"w" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease]; NSArray *sortDescriptors = [NSArray arrayWithObject: sorter]; [mGlossaryArray sortUsingDescriptors:sortDescriptors]; 
+166
Apr 04 2018-11-18T00:
source share

Just use the NSSortDescriptor as I used and it worked perfectly.

  NSSortDescriptor * sortByRank = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]; 
+38
Feb 15 '13 at 3:49
source share

May I suggest using -localizedStandardCompare: (NSString)?

"This method should be used whenever file names or other lines are presented in lists and tables where sorting by search query is suitable. The exact sorting behavior of this method is different in different locales and may be changed in future releases."

+5
Jan 24 '12 at 13:05
source share

You can use this to sort the array by name, which also contains a small letter :

 NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:@"w" ascending:YES selector:@selector(caseInsensitiveCompare:)]; NSArray *sortDescriptors = [NSArray arrayWithObject:sorter]; [mGlossaryArray sortUsingDescriptors:sortDescriptors]; 

This code is great for sorting a name according to alphabets, which are also of a small character, such as rocky, Ajay, john, Bob, etc.

+3
Sep 03
source share

I think this will help you. The documents for this are here: String Programming Guide

Add this little feature 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 you copy the function definition into your header, or you get a compilation error in a sorted array.

For your sorted array, use this method:

 [mGlossaryArray sortedArrayUsingFunction:finderSortWithLocale context:[NSLocale currentLocale]]; 

Your results will look like this:

  • from
  • cabin
  • Cafe
  • Cancer
  • Chinese
  • Christianity
  • Christmas
  • Coke
+2
Apr 05 2018-11-11T00:
source share

This code works 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]; } 
+2
Feb 09 '12 at 11:13
source share

An alternative kind of Apple finder sorting using the locale method uses a comparator block, useful if you are in an ARC environment and don’t want to deal with jump bridges, 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 would also recommend storing the current locale in a local variable for efficiency.

+2
Jun 28 '13 at 19:51
source share



All Articles