# 求排序算法高手,在线等

0

1 using System.Text.RegularExpressions;
2 class obj {
3     public string cwdm;
4     public override string ToString()
5     {
6         return cwdm;
7     }
8 }
9
10 var regex = new Regex("[0-9]+");
11 var beds = new List<obj> { new obj { cwdm = "+002" }, new obj { cwdm = "+001" }, new obj { cwdm = "003" }, new obj { cwdm = "001" }, new obj { cwdm = "002" }, new obj { cwdm = "+005" }, };
12 Console.WriteLine(string.Join("*", beds));
13 beds.Sort((x, y) => {
14     int bx = 0, by = 0;
15     bool rx = int.TryParse(x.cwdm, out bx), ry = int.TryParse(y.cwdm, out by);
16     Match mx = regex.Match(x.cwdm),my = regex.Match(y.cwdm);
17
18     if (rx && ry) return bx - by;
19     if (!rx && !ry && mx != null && mx.Success && my != null && my.Success) return  int.Parse(mx.Value) - int.Parse(my.Value);
20     else if (ry) return 1;
21     else if (rx) return 0;
22
23
24     return x.cwdm.CompareTo(y.cwdm);
25 });
26
27 Console.WriteLine(string.Join("*", beds));
i.Net | 初学一级 | 园豆：54

您需要登录以后才能回答，未注册用户请先注册