C# Sorting alphabetical order a - z and then to aa, ab - zz

This should do it.

var data = new List<string>() { "a", "b", "f", "aa", "z", "ac", "ba" };
var sorted = data.OrderBy(x => x.Length).ThenBy(x => x);

Result:

a, b, f, z, aa, ac, ba


If you are looking to actually order an existing list, you likely want to use the OrderBy() series of methods (e.g. OrderBy(), OrderByDescending(), ThenBy(), ThenByDescending()):

var orderedList = yourList.OrderBy(x => x.Length)
                          .ThenBy(x => x);

Example

You can find a working, interactive example here which would output as follows:

a,b,f,z,aa,ac,ba


This will sort your List of strings first by Length and then by alphabetical order

List<string> sorted = original.OrderBy(x => x.Length).ThenBy(x => x).ToList();

Tags:

C#

Linq