Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort an array of strings alphabetically when the strings contains åäö?

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

The orded array should look something like this: 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.

And when I use localizedCompare the order change to: 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.

like image 321
user1279526 Avatar asked Mar 28 '12 17:03

user1279526


People also ask

How do you sort an array of strings in alphabetical order?

Given a string str and an array of strings strArr [], the task is to sort the array according to the alphabetical order defined by str . Note: str and every string in strArr [] consists of only lower case alphabets.

What is the default order of sorting in JavaScript?

By default, the sort order is ascending, built upon converting the elements into strings and then comparing their sequences of UTF-16 code unit values. Number sorting can be incorrect as the sort ( ) method sorts numbers in the following order: "35" is bigger than "225", because "3" is bigger than "2".

How to sort an array in ascending order using comparator?

A Comparator is needed as a third argument in sort function to specify the order in which array is to be sorted. A comparator always has a return type bool and always takes two arguments. If we have to sort it in ascending order, we will use the below format: bool comparator(string a,string b)

How to change the alphabetical order of a string in Python?

Approach: Traverse every character of str and store the value in a map with character as the key and its index in the array as the value . Now, this map will act as the new alphabetical order of the characters.


1 Answers

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

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
like image 71
Nick Moore Avatar answered Sep 19 '22 15:09

Nick Moore