ios - sorting NSArray of strings with numbers -


this question has answer here:

i have array contains these string.

@"common area", @"building 1", @"building 2", @"building 3", @"building 4", @"building 10", @"building 14", @"car park", 

i trying sort alphabetically this

areaarray = [areaarray sortedarrayusingselector:@selector(localizedcaseinsensitivecompare:)]; 

but displayed like

@"building 1", @"building 10", @"building 14", @"building 2", @"building 3", @"building 4", @"car park", @"common area", 

where displayed as

@"building 1", @"building 2", @"building 3", @"building 4", @"building 10", @"building 14", @"car park", @"common area", 

this code sample magic.

nsarray *stringsarray = @[@"common area", @"building 1", @"building 2", @"building 3", @"building 4", @"building 10", @"building 14", @"car park"];   static nsstringcompareoptions comparisonoptions = nscaseinsensitivesearch | nsnumericsearch | nswidthinsensitivesearch | nsforcedorderingsearch;  nslocale *currentlocale = [nslocale currentlocale];  nscomparator findersortblock = ^(id string1, id string2) {     nsrange string1range = nsmakerange(0, [string1 length]);     return [string1 compare:string2 options:comparisonoptions range:string1range                      locale:currentlocale]; };  nsarray *findersortarray = [stringsarray sortedarrayusingcomparator:findersortblock];  nslog(@"findersortarray: %@", findersortarray); 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -