我有一个清单
var x = new List() { "a", "1", "b", "2" }; var sortedResultByNumeric = x .Select(s => new { OriginalString = s, ExtractNumbers = String.Join("", s.Where(Char.IsDigit)) }) .OrderBy(o => o.ExtractNumbers).ToList();
输出是
a b 1 2
但预计是
1 2 a b
我怎么能这样做?
你看到的和预期的输出是基于OriginalString
因为ExtractNumbers
是"","","1","2"
那么你应该OrderBy
OriginalString
:
var x = new List() { "a", "1", "b", "2" }; var sortedResultByNumeric = x .Select(s => new { OriginalString = s, ExtractNumbers = String.Join("", s.Where(Char.IsDigit)) }) .OrderBy(o => o.OriginalString).ToList();
输出:
1
2
一个
b