Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to sort an array using NSSortDescriptor

I am having a problem regarding sorting an array w.r.t database:

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

Here in database there are some first capital letters and because of that capital letter it does not show me proper sorted output. Here i am sorting an array with r.t "w" which is my table column in database. Here I have attach the screen shot for the output, which says that "Cancer" comes first than "c", but this is not correct, it is not giving alphabetically sort because of the capitalized words.

eg. if there is "able" in lower case and "aCid" then it will show aCid first and then able, and there is also a case where if the 1st letter is caps it comes first eg, "Able" and "a". Here Able displays first.enter image description here

like image 897
Prash....... Avatar asked Apr 04 '11 18:04

Prash.......


2 Answers

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];  
like image 147
Hobbes the Tige Avatar answered Sep 20 '22 13:09

Hobbes the Tige


Just use NSSortDescriptor like I used and It worked fine.

   NSSortDescriptor * sortByRank = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]; 
like image 24
Prakash Avatar answered Sep 22 '22 13:09

Prakash