c# - LINQ order by alphabetical followed by empty string

ID : 274502

viewed : 31

Tags : c#linqc#





Top 4 Answer for c# - LINQ order by alphabetical followed by empty string

vote vote

90

You could use something like:

var result = new[] { "a", "c", "", "b", "d", }     .OrderBy(string.IsNullOrWhiteSpace)     .ThenBy(s => s);   //Outputs "a", "b", "c", "d", "" 
vote vote

81

Alternatively to the existing answers, you could provide an IComparer<string> to the OrderBy overload:

class Program {     static void Main(string[] args)     {         var letters = new[] {"b", "a", "", "c", null, null, ""};          var ordered = letters.OrderBy(l => l, new NullOrEmptyStringReducer());          // Results: "a", "b", "c", "", "", null, null          Console.Read();     } }  class NullOrEmptyStringReducer : IComparer<string> {     public int Compare(string x, string y)     {         var xNull = x == null;         var yNull = y == null;          if (xNull && yNull)             return 0;          if (xNull)             return 1;          if (yNull)             return -1;          var xEmpty = x == "";         var yEmpty = y == "";          if (xEmpty && yEmpty)             return 0;          if (xEmpty)             return 1;          if (yEmpty)             return -1;          return string.Compare(x, y);     } } 

I don't state that this is a good example of the IComparer implementation (it probably needs to do null checking and handle if both strings are empty), but the point of the answer is to demonstrate the OrderBy overload and at the very least works with the question's sample data.

Due to feedback in the comments and my own curiosity, I've provided a slightly more involved implementation that also takes care of ordering empty strings and null strings relative to each other. Whitespace is not handled.

Still, the point is the ability to provide IComparer<string>, not how well you choose to write it :-)

vote vote

74

string[] linqSort = { "", "c","x", "a","" ,"b","z" }; var result = from s in linqSort              orderby  string.IsNullOrEmpty(s),s              select s; 
vote vote

63

Top 3 video Explaining c# - LINQ order by alphabetical followed by empty string







Related QUESTION?