Sort List like Excel columns sort

后端 未结 2 938
醉话见心
醉话见心 2020-12-21 07:38

Please consider this List:

\"A\", \"C\", \"AB\", \"AD\", \"N\", \"Z\", \"AC\"

I want to sort this string (That are being Excel column) like

2条回答
  •  鱼传尺愫
    2020-12-21 08:03

    First order by the length of column name ("C" comes before "AB"), then use normal alphabetical (string) sorting on strings with same length ("AC" before "AD").

    var columns = new[] { "A", "C", "AB", "AD", "N", "Z", "AC" };
    var sorted = columns.OrderBy(c => c.Length)
                        .ThenBy(c => c);
    

提交回复
热议问题