Search code examples
c#sortingunicodecjk

Sorting Japanese text by Katakana in C#


Is it possible to sort a list of Japanese strings by their Katakana?


Solution

  • Sure you can. If you use CultureInfo, you can make it so it doesn't bother looking for upper/lower-case.

    // Create CultureInfo
    System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("ja-JP");
    //Create the StringComparer
    StringComparer cmp = StringComparer.Create(ci, true);
    
    // Sort your array of string
    Array.Sort(myArray, cmp);
    

    You can extend the functionality of the sort to not distinguish between Hiragana and Katakana if you like by doing this:

    //Create CultureInfo
    System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("ja-JP");
    // Set it so it ignores the kana type
    CultureInfoCompare cmp =
    new CultureInfoCompare(ci, System.Globalization.CompareOptions.IgnoreKanaType);
        
    //Sort it
    Array.Sort(myArray, cmp);
    

    頑張ってください!