Search code examples
c#stringcomparer

Any reference that confirms that Array.Sort(charArray, StringComparer.Ordinal) works?


I would like to sort an array of chars according to their ordinal positions (code points) in Unicode table.

I see that the following code works:

char[] charArray = new[] { 'h', 'e', 'l', 'l', 'o' };
Array.Sort(charArray, StringComparer.Ordinal);

But it looks a bit weird. First because both of these parameters are non-generic, and secondly, here I am using a StringComparer to compare chars.

Is this guaranteed to work? Any reference?


Solution

  • OrderBy(chr => chr) does the trick. char is IComparable and that comparable definition compares the integer/"ordinal" value of the chars.