Sorting List in C#

后端 未结 7 1662
囚心锁ツ
囚心锁ツ 2020-11-28 12:01

How to sort a List based on the item\'s integer value

The list is like

\"1\"
\"5\"
\"3\"
\"6\"
\"11\"
\"9\"
\"NUM1\"
\"NUM0\"

The r

7条回答
  •  时光说笑
    2020-11-28 12:51

    Here is a C# 7 solution (assuming the list has the name a):

        var numericList = a.Where(i => int.TryParse(i, out _)).OrderBy(j => int.Parse(j)).ToList();
        var nonNumericList = a.Where(i => !int.TryParse(i, out _)).OrderBy(j => j).ToList();
        a.Clear();
        a.AddRange(numericList);
        a.AddRange(nonNumericList);
    

提交回复
热议问题