IComparer example, with numbers first, and empty strings at the end:
using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace IComparer { class Program { public class myCaseInsensitiveComparer : IComparer { public int Compare(string x, string y) { bool isXnumeric = int.TryParse(x, out var xInt); bool isYnumeric = int.TryParse(y, out var yInt); if (string.IsNullOrWhiteSpace(x) && string.IsNullOrWhiteSpace(y)) { return 0; } if (string.IsNullOrWhiteSpace(x)) { return 1; } if (string.IsNullOrWhiteSpace(y)) { return -1; } if (isXnumeric && isYnumeric) { return xInt.CompareTo(yInt); } return string.Compare(x, y, StringComparison.OrdinalIgnoreCase); } } static void Main(string[] args) { string[] words = { "1", "a", "A", "", "b", "", "B", "C", "c", "", "", "3" }; IOrderedEnumerable sortedWords = words.OrderBy(a => a, new myCaseInsensitiveComparer()); foreach (var sortedWord in sortedWords) { Console.WriteLine(sortedWord); } Console.WriteLine("Press any key..."); Console.ReadKey(); } } }
Result should be something like:
Source you can download from here.