ios - sorting NSArray of strings with numbers -
this question has answer here:
- how natural sort on nsarray? 5 answers
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
Post a Comment