当字符串包含åäö时,如何按字母顺序对字符串数组进行排序?

时间:2022-02-27 03:19:21

I'm building an app in xcode4.3/Objective-C and have come across a problem when trying to sort an NSMutableArray. I'll populate it with strings from a sqlite database. The problem occurs with the swedish characters å, ä and ö.

我正在xcode4.3 / Objective-C中构建一个应用程序,并且在尝试对NSMutableArray进行排序时遇到了问题。我将使用sqlite数据库中的字符串填充它。问题出现在瑞典人物å,ä和ö上。

The orded array should look something like this: as, br, ol, st, år, ög, ös.

orded数组看起来像这样:as,br,ol,st,år,ög,ös。

But when I use the selector compare the order is this: as, år, br, ol, ög, ös, st.

但是当我使用选择器比较顺序是这样的:as,år,br,ol,ög,ös,st。

And when I use localizedCompare the order change to: as, år, br, ög, ol, ös, st.

当我使用localizedCompare时,订单更改为:as,år,br,ög,ol,ös,st。

According to older threads the localizedCompare should be the solution, but I can't make it work correctly. If I use the terminal to access the sqlite database and type ORDER I'll get the correct result. Could my problem be related to some settings in xcode or the iphone simulator, since neither display the correct order? Or is localizedCompare the wrong way to go? I'll happily accept any workarounds as long as it gets the job done. Thanks.

根据较旧的线程,localizedCompare应该是解决方案,但我无法使其正常工作。如果我使用终端访问sqlite数据库并输入ORDER,我将得到正确的结果。我的问题可能与xcode或iphone模拟器中的某些设置有关,因为它们都没有显示正确的顺序?或者本地化比较错误的方式去?只要完成工作,我就会高兴地接受任何变通办法。谢谢。

1 个解决方案

#1


13  

You can make it work by using compare:options:range:locale: and specifying Swedish locale explicitly, like this:

您可以使用compare:options:range:locale:并明确指定Swedish locale,使其工作,如下所示:

NSArray *strings=[NSArray arrayWithObjects:@"as", @"ol", @"st", @"br", @"ög", @"år", @"ös", nil];
NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifier:@"sv_SE"];

NSArray *sorted=[strings sortedArrayUsingComparator:^(NSString *first, NSString *second) {
    return [first compare:second
                  options:0
                    range:NSMakeRange(0, [first length])
                   locale:locale];
}];

for (NSString *s in sorted) { NSLog(@"%@", s); }

The output is:

输出是:

2012-04-10 08:08:18.139 Untitled[32416:707] as
2012-04-10 08:08:18.140 Untitled[32416:707] br
2012-04-10 08:08:18.141 Untitled[32416:707] ol
2012-04-10 08:08:18.142 Untitled[32416:707] st
2012-04-10 08:08:18.142 Untitled[32416:707] år
2012-04-10 08:08:18.143 Untitled[32416:707] ög
2012-04-10 08:08:18.143 Untitled[32416:707] ös

#1


13  

You can make it work by using compare:options:range:locale: and specifying Swedish locale explicitly, like this:

您可以使用compare:options:range:locale:并明确指定Swedish locale,使其工作,如下所示:

NSArray *strings=[NSArray arrayWithObjects:@"as", @"ol", @"st", @"br", @"ög", @"år", @"ös", nil];
NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifier:@"sv_SE"];

NSArray *sorted=[strings sortedArrayUsingComparator:^(NSString *first, NSString *second) {
    return [first compare:second
                  options:0
                    range:NSMakeRange(0, [first length])
                   locale:locale];
}];

for (NSString *s in sorted) { NSLog(@"%@", s); }

The output is:

输出是:

2012-04-10 08:08:18.139 Untitled[32416:707] as
2012-04-10 08:08:18.140 Untitled[32416:707] br
2012-04-10 08:08:18.141 Untitled[32416:707] ol
2012-04-10 08:08:18.142 Untitled[32416:707] st
2012-04-10 08:08:18.142 Untitled[32416:707] år
2012-04-10 08:08:18.143 Untitled[32416:707] ög
2012-04-10 08:08:18.143 Untitled[32416:707] ös