Home All Groups Group Topic Archive Search About

Culture sensitive sorting doesn't appear to work

Author
8 Mar 2006 4:06 PM
lportx
Hi there:

I am looking into collation for various cultures in attempt to make our
web app sort correctly in multiple languages.  I have tested the
COLLATION keywork in sql server, and this works fine.

However, the .Net stuff does not sort spanish language any differently
that english: the 'ch' letter is not alphabetized after the letter 'c'.

Any ideas why this might be?

Below is the code I am using to test out String.Compare, Array.Sort,
and SortKey.Compare, none of which work as I expected:

    string[] names = { "can", "cab", "chanel", "coco" };

    // Sort in en-US culture
    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    Array.Sort(names);
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("Sorting array in en-US culture");
    foreach (String name in names)
    {
      sb.AppendLine(name);
    }
    int result1 = String.Compare("coco", "chanel");
    sb.AppendLine("String.Compare: result of coco vs. chanel = " +
result1.ToString());

    SortKey sc1 =
Thread.CurrentThread.CurrentCulture.CompareInfo.GetSortKey("coco");
    SortKey sc2 =
Thread.CurrentThread.CurrentCulture.CompareInfo.GetSortKey("chanel");
    // Compares the two sort keys and display the results.
    int result2 = SortKey.Compare(sc1, sc2);
    sb.AppendLine(string.Format("\nWhen the CurrentCulture is
\"en-US\",\nthe " +
            "result of comparing {0} with {1} is: {2}", "coco",
"chanel",
            result2));

    Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");
    Array.Sort(names);
    sb.AppendLine("Sorting array in es-ES culture");
    foreach (String name in names)
    {
      sb.AppendLine(name);
    }
    result1 = String.Compare("coco", "chanel");
    sb.AppendLine("result of coco vs. chanel = " + result2.ToString());

    sc1 =
Thread.CurrentThread.CurrentCulture.CompareInfo.GetSortKey("coco");
    sc2 =
Thread.CurrentThread.CurrentCulture.CompareInfo.GetSortKey("chanel");
    // Compares the two sort keys and display the results.
    result2 = SortKey.Compare(sc1, sc2);
    sb.AppendLine(string.Format("\nWhen the CurrentCulture is
\"es-ES\",\nthe " +
            "result of comparing {0} with {1} is: {2}", "coco",
"chanel",
            result2));

Here is the output from this program:

Sorting array in en-US culture
cab
can
chanel
coco
String.Compare: result of coco vs. chanel = 1
When the CurrentCulture is "en-US",
the result of comparing coco with chanel is: 1

Sorting array in es-ES culture
cab
can
chanel
coco
result of coco vs. chanel = 1
When the CurrentCulture is "es-ES",
the result of comparing coco with chanel is: 1

AddThis Social Bookmark Button