我怎么可以按字母顺序在字符串中包含AAO排序字符串数组? [英] How can I sort an array of strings alphabetically when the strings contains åäö?

查看:117
本文介绍了我怎么可以按字母顺序在字符串中包含AAO排序字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建在X code4.3 / Objective-C的应用程序,并试图理清一个NSMutableArray时所遇到的一个问题。我会从SQLite数据库字符串填充它。与瑞典字符,A型和O出现问题。

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

该orded数组应该是这个样子:如,BR,醇,ST,AR,OG,操作系统。

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

但是,当我使用选择比较的顺序是这样的:因为,AR,BR,醇,OG,操作系统,ST

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

当我使用localizedCompare顺序变化:如,AR,BR,OG,醇,操作系统,ST

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

根据旧的线程localizedCompare应该是解决方案,但我不能让它正常工作。如果我使用终端访问SQLite数据库和类型顺序,我会得到正确的结果。难道我的问题涉及到在X code或iPhone模拟器一些设置,因为无论显示正确的顺序?或者是localizedCompare错误的方式去?我高兴地会,只要它能够完成任务接受任何变通办法。谢谢你。

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.

推荐答案

您可以把它用工作比较:选项:范围:区域:,并明确指定瑞典的语言环境,像这样的:

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

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); }

输出是:

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

这篇关于我怎么可以按字母顺序在字符串中包含AAO排序字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆