Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort List by localization

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

like image 777
Muhammad Nour Avatar asked Sep 16 '25 20:09

Muhammad Nour


1 Answers

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.

like image 60
Elian Ebbing Avatar answered Sep 18 '25 17:09

Elian Ebbing