获取 localizedStandardCompare 进行比较时使用的字符串
我使用 localizedStandardCompare:
对数组进行排序,它为我提供了以正确的本地化顺序排序的对象。现在我想在 UITableView 的 index
中使用它们,这意味着我需要生成部分标题。由于 localizedStandardCompare:
对它们进行了本地化,因此以“Æ”开头的字符串会与 A 对象一起排序。
因此,如果我迭代数组并为每个第一个字母创建一个部分,我将得到“A”、“Æ”、“A”、“B”等部分。有没有办法获取用于的字符串比较,或者至少模仿它?我尝试过使用不同的编码进行转换,但没有一个会返回“A”表示“Æ”。
关于如何生成本地化字符串的部分标题还有其他建议吗?
I'm sorting an array by using localizedStandardCompare:
, which gives me the objects sorted in the correct, localized order. Now I want to use them in the index
for a UITableView
, which means I need to generate the section titles. Since localizedStandardCompare:
localizes them, a string that starts with "Æ" is sorted with the A objects.
So if I iterate the array and make a section for each first letter, I'll get sections "A", "Æ", "A", "B", etc. Is there a way to get the string that was used for comparison, or at least emulate it? I've tried converting with different encodings, but none will return me an "A" for "Æ".
Any other suggestions for how to generate the section titles for localized strings?
您可以使用
CFStringTransform
方法来获取使用的字符串。例如,使用 kCFStringTransformToLatin 值从输入字符串中获取简单的拉丁字符串。
这实际上与
localizedStandardCompare:
方法的行为并不完全相同,但您可以使用字符串转换为用于实际排序的对象生成附加属性。关于这个主题有很多好的资源:
You can use the
CFStringTransform
method to get the used string.For example use the
kCFStringTransformToLatin
value to get a simple latin string from the input string.This won't actually behave exactly as the
localizedStandardCompare:
method, but you can use the string transform to generate an additional attribute for the object which you use for the actual sorting.There are many good resources about this topic: