Sorting array containing strings in objective c
- by jakob
Hello experts!
I have an array named 'names' with strings looking like this:
["name_23_something", "name_25_something", "name_2_something"];
Now I would like to sort this array in ascending order so it looks like this:
["name_25_something", "name_23_something", "name_2_something"];
I guess that should start of with extracting the numbers since I want that
the sorting is done by them:
for(NSString *name in arr) {
NSArray *nameSegments = [name componentsSeparatedByString:@"_"];
NSLog("number: %@", (NSString*)[nameSegments objectAtIndex:1]);
}
I'm thinking of creating a dictionary with the keys but I'm not sure if that is the correct objective-c way, maybe there some some methods I could use instead? Could you please me with some tips or example code how this sorting should be done in a proper way.
Thank you