Sort UITableView in Russian alphabetical order
I'm a noob, and I'm sorry because the question is really stupid. I have An NSArray which contains titles for UITableView rows in NSDictionaries in Russian an开发者_StackOverflow中文版d I need to sort these titles in alphabetical order. How can I do it?
Help me, please.
Thanks in advance!
On iOS 4.0 and later, you can use a sort descriptor. Assuming title
is the key under which your title strings are stored in the dictionaries:
NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:
[NSArray arrayWithObject:
[NSSortDescriptor sortDescriptorWithKey:@"title"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)]]];
Another option is to use a block-based sort method:
NSArray *sortedArray = [myArray sortedArrayUsingComparator:^(id obj1, id obj2) {
NSString *string1 = [(NSDictionary *)obj1 objectForKey:@"title"];
NSString *string2 = [(NSDictionary *)obj2 objectForKey:@"title"];
return [string1 localizedCaseInsensitiveCompare:string2];
}];
If you need your app to run on iOS 3.1.3 and earlier, however, you can either:
- write a comparison method as a category on NSDictionary and pass it to
-sortedArrayUsingSelector:
, or - write a comparison function and pass it to
-sortedArrayUsingFunction:context:
.
In each case, your method or function's body will be essentially the same as the body of the block in the second example above. The NSArray class reference contains examples of both techniques.
I think this will sort your array:
NSArray *sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
精彩评论