objective c - Sorting array(NSString + Number) using NSSortDescriptor in IOS? -


i want sort array using nssortdescriptor.

here code

 nssortdescriptor *descriptor=[[nssortdescriptor alloc] initwithkey:@"filename"  ascending:yes selector:@selector(localizedcaseinsensitivecompare:)]; [arrdocuments sortusingdescriptors:[nsarray arraywithobjects:descriptor,nil]]; 

what incorrect result.

  1. new folder 1
  2. new folder 11
  3. new folder 12
  4. new folder 2

expected

  1. new folder 1
  2. new folder 2
  3. new folder 11
  4. new folder 12

the function localizedcaseinsensitivecompare: alphabetic search.

you better using function like...

compare:options: 

with options nsnumericsearch treats numbers numeric , sorts them 1, 2, 10, etc...

alphabetically though 10 comes before 2 hence problem.

the entire code like...

nssortdescriptor *sd = [nssortdescriptor sortdescriptorwithkey:@"filename" ascending:yes comparator:^(nsstring *obj1, nsstring *obj2) {      return [obj1 compare:obj2 options:nsnumericsearch | nscaseinsensitivesearch];  }];  [arrdocuments sortusingdescriptors:@[sd]]; 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -