简体   繁体   中英

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 and 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.

我认为这将对您的数组进行排序:

NSArray *sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM