How to sort case insensitive with System.Dynamic.Linq?

You do not need to create a custom comparer because there's already a StringComparer class which derives from IComparer.

words.OrderBy (x => x, StringComparer.OrdinalIgnoreCase)

This way, you do not need to create different IComparer implementations if you wanted to use other string comparison methods, like StringComparer.InvariantCultureIgnoreCase.

However, this might be desirable depending on your situation. For example, I do have multiple extension methods defined in LINQPad, like OrderBySelfInvariantCultureIgnoreCase, because it is convenient to use this with code completion rather than typing out the equivalent code by hand:

public static IEnumerable<string> OrderBySelfInvariantCultureIgnoreCase(this IEnumerable<string> source)
{   
    return source.OrderBy (x => x, StringComparer.InvariantCultureIgnoreCase);
}

Tags:

C#

Linq