Friday, September 17, 2010

Some words on Norwegian letters

First as a proud Norwegian, I must say this problem hadn’t been there if not the Swedish also had their own letters. We Scandinavians always have jokes about the Norwegian, Swedish and Danish who did something, and the better man is alway the Norwegian here in Norway. Likewise in the other countries.

The Danish and Norwegian alphabets end with æøå, whereas the Swedish and the Finnish ones conventionally put åäö at the end. The website I do some development for had this page with a list of proprietors.

screenshot

The heading displays all 29 Norwegian letters. Clicking on one of the letters causes the list to only display proprietors starting with this letter. The letter is given in the url with a postback. Like this at the end of the url; “&letter=68” for the the letter D.

The codebehind looked like this:

1 proprietors = proprietors.Where(x => x.Name.StartsWith(Convert.ToString(currentLetter)));

The problem with this were names which starts with letters other than the Norwegian. In this case a proprietor starting with the Swedish Ä, which didn’t show up either beneath A, Æ or Å. And with CultureInfo set to Norwegian, I would say Ä and Æ should be equal.

I was hoping the framework could offer me a solution for my problem. With an operation that could thell me that Ä equals Æ for Norwegian culture, and even A equals Ä equals Æ equals Å for CultureInfo(“en-US”). But I could not find such methode. Please let me know if you have any suggestions or ideas about this?



So what I had to is a more complex Linq where I do something almost like a sort. And then pick every name equal or larger than the current letter, but lower than the next letter. This way I get hold of Ä which is placed here sorting with Norwegian culture“…XYZÆÄØÅ”. This way finally the Swedish company showed up:



image



The code ended up like this:



1 //reduce list to all with letters larger or equal currentLetter
2 var fromChar = currentLetter == Alphabet.First() ? StartChar : currentLetter;
3 proprietors = proprietors.Where(proprietor => string.Compare(proprietor.Name.First().ToString(), fromChar.ToString(), true, new CultureInfo(AlphabetCulture)) >= 0);
4
5 //reduce list to all with letters smaller than the next letter in alphabet
6 var nextChar = NextLetter(currentLetter);
7 proprietors = proprietors.Where(proprietor => string.Compare(proprietor.Name.First().ToString(), nextChar.ToString(), true, new CultureInfo(AlphabetCulture)) < 0).ToList();
8

With theese supporting methods and constants:

1 private char currentLetter;
2 const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ";
3 const string AlphabetCulture = "nb-no";
4 private const char EndChar = '\uffff';
5 private const char StartChar = '\u0000';
6
7 /// <summary>
8 /// Returns the next letter in <see cref="Alphabet"/>. If latest letter, 'Å', then return the larges Unicode char.
9 /// </summary>
10 /// <param name="letter">The letter.</param>
11 /// <returns></returns>
12 private char NextLetter(char letter)
13 {
14 var index = Alphabet.IndexOf(letter);
15 return index + 1 < Alphabet.Length ? Alphabet.ElementAt((index + 1)) : EndChar;
16 }


If you got any feedback on how to this this better and more generic, please let me know! Thanks.

No comments:

Post a Comment