4

i'm paging countries in an alfabet, so countries starting A-D, E-H etc. But i also want to list åbrohw at the a, and ëpollewop at the e. I tried string.startswith providing a stringcompare option, but it doesn't work...

i'm running under the sv-SE culture code, if that matters...

Michel

bdukes
  • 152,002
  • 23
  • 148
  • 175
Michel
  • 23,085
  • 46
  • 152
  • 242
  • Isn't "How to remove diacritic marks from characters" a better title? Just suggesting this, don't want to modify a good title into something slightly better. – Wim ten Brink Sep 15 '09 at 14:31

4 Answers4

6

See How do I remove diacritics (accents) from a string in .NET? for the solution to create a version without the diacritics, which you can use for the comparisons (while still displaying the version with the diacritics).

Community
  • 1
  • 1
bdukes
  • 152,002
  • 23
  • 148
  • 175
4

Oh yes, the culture matters. If you run the following:

List<string> letters = new List<string>() { "Å", "B", "A" };

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("sv-SE");
letters.Sort();
Console.WriteLine("sv-SE:")
letters.ForEach(s => Console.WriteLine(s));   

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-GB");
letters.Sort();
Console.WriteLine("en-GB:")
letters.ForEach(s => Console.WriteLine(s));

...you get the following output:

sv-SE:
A
B
Å
en-GB:
A
Å
B
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • +1 as I was going to answer - just use a culture that considers "A" and "Å" to be the same letter. – Rex M Sep 15 '09 at 14:37
1

Try using range selection instead of precise matching.

A: (firstLetter <= A)
B: (firstLetter > A) AND (firstLetter <= B)
...
1

You would have to give a specific culture for sorting or write your own comparer for that. Default sorting order for Swedish puts å, ä, ö at the end.

Most likely you would like to decompose letters with diacritics and sort them as if they wouldn't have a diacritic mark.

Joey
  • 344,408
  • 85
  • 689
  • 683