Translate

Tuesday 3 May 2016

Sorting mixed numbers and strings

split the sequence into numbers and non-numbers, then sort each subgroup, finally join the sorted results; something like:
var input = new[] {"a", "1", "10", "b", "2", "c"};

var result = input.Where( s => s.All( x => char.IsDigit( x ) ) )
                  .OrderBy( r => { int z; int.TryParse( r, out z ); return z; } )
                  .Union( input.Where( m => m.Any( x => !char.IsDigit( x ) ) )
                               .OrderBy( q => q ) );