Sort List by localization

前端 未结 2 1388
日久生厌
日久生厌 2021-01-06 19:57

I need to sort a List which contains Japanese alphabet. How could I do this in C#?

2条回答
  •  死守一世寂寞
    2021-01-06 20:33

    There is an overload List.Sort(IComparer comparer). You can pass a culture specific comparer to the sort method. The following code compares using the Japanese culture settings:

    myList.Sort(StringComparer.Create(new CultureInfo("ja-JP"), true));
    

    In this case I passed true as the argument to indicate that the comparison must be case insensitive. The StringComparer has a couple of static properties and methods to create a suitable comparer:

    StringComparer.CurrentCulture;
    StringComparer.CurrentCultureIgnoreCase;
    StringComparer.Create(CultureInfo culture, bool ignoreCase);
    etc.
    

    You can find more information on this msdn page.

提交回复
热议问题