开发者

Sort List by localization

开发者 https://www.devze.com 2023-02-21 20:36 出处:网络
I need to sort a List<string> which contains Jap开发者_JAVA技巧anese alphabet. How could I do this in C#?There is an overload List<T>.Sort(IComparer<T> comparer). You can pass a cult

I need to sort a List<string> which contains Jap开发者_JAVA技巧anese alphabet. How could I do this in C#?


There is an overload List<T>.Sort(IComparer<T> 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.


I use:

Array.Sort(myArray, StringComparer.Ordinal);

It will sort the array by Ascii value, so you'll get hiragana, katakana, and then kanji.

0

精彩评论

暂无评论...
验证码 换一张
取 消