使用特殊字符对数组进行排序 - iPhone

发布于 2024-08-26 06:28:16 字数 376 浏览 4 评论 0原文

我有一个带有法语字符串的数组,可以说:“égrener”和“exact”我想对其进行排序,例如 égrener 是第一个。当我这样做时:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:descriptor];
NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];

我在列表末尾看到 é...我该怎么办?

谢谢

I have an array with french strings let say: "égrener" and "exact" I would like to sort it such as égrener is the first. When I do:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:descriptor];
NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];

I get the é at the end of the list... What should I do?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话墙 2024-09-02 06:28:16

NSString 中有一个方便的方法,可以让您轻松地进行这种类型的排序:

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

NSString 的底层比较方法 (< code>compare:options:range:locale:) 为您提供了更多关于如何排序的选项。

编辑:这是一个长篇故事:

首先,定义一个比较函数。这对于自然字符串排序很有用:

static NSInteger comparator(id a, id b, void* context)
{
    NSInteger options = NSCaseInsensitiveSearch
        | NSNumericSearch              // Numbers are compared using numeric value
        | NSDiacriticInsensitiveSearch // Ignores diacritics (â == á == a)
        | NSWidthInsensitiveSearch;    // Unicode special width is ignored

    return [(NSString*)a compare:b options:options];
}

然后,对数组进行排序。

    NSArray* myArray = [NSArray arrayWithObjects:@"foo_002", @"fôõ_1", @"fôõ_3", @"foo_0", @"foo_1.5", nil];
    NSArray* sortedArray = [myArray sortedArrayUsingFunction:comparator context:NULL];

示例中的数组包含一些有趣的字符:数字、变音符号以及 unicode 范围 ff00 中的一些字符。最后一种字符类型看起来像 ASCII 字符,但以不同的宽度打印。

使用的比较函数以人类可预测的方式处理所有情况。排序后的数组具有以下顺序:

foo_0
fôõ_1
foo_1.5
foo_002
fôõ_3

There’s a convenience method in NSString that lets you do this type of sorting easily:

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

NSString’s underlying comparison method (compare:options:range:locale:) gives you even more options of how sorting should be done.

Edit: Here’s the long story:

First, define a comparison function. This one is good for natural string sorting:

static NSInteger comparator(id a, id b, void* context)
{
    NSInteger options = NSCaseInsensitiveSearch
        | NSNumericSearch              // Numbers are compared using numeric value
        | NSDiacriticInsensitiveSearch // Ignores diacritics (â == á == a)
        | NSWidthInsensitiveSearch;    // Unicode special width is ignored

    return [(NSString*)a compare:b options:options];
}

Then, sort the array.

    NSArray* myArray = [NSArray arrayWithObjects:@"foo_002", @"fôõ_1", @"fôõ_3", @"foo_0", @"foo_1.5", nil];
    NSArray* sortedArray = [myArray sortedArrayUsingFunction:comparator context:NULL];

The array in the example contains some funny characters: Numbers, diacritical marks, and some characters from unicode range ff00. The last character type looks like an ASCII character but is printed in a different width.

The used comparison function handles all cases in human predictable way. The sorted array has the following order:

foo_0
fôõ_1
foo_1.5
foo_002
fôõ_3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文