看到你之前回复的一个帖子
http://bbs.csdn.net/topics/350096947
我目前也遇到了类似的问题,有如下数组string[] fileList = new string[] { "123456-asd45sdf-2", "6666-a234sdsdf-5", "7849561-asdsdf-1" };想让数组中每个元素按第三个“-”后面的数字降序排序输出效果:
6666-a234sdsdf-5
123456-asd45sdf-2
7849561-asdsdf-1之前看到你写的代码fileList.OrderBy(s => int.Parse(Regex.Match(s, @"\d+").Value)).ToArray();本想将代码套用到我遇到的问题上,不过run了一下发现不可行,因为我的元素中包含多个数字,但我遇到的需求是将元素按第二个“-”后面数字降序排序,麻烦帮下忙。多谢了~

解决方案 »

  1.   

    fileList.OrderBy(s => int.Parse(Regex.Match(s, @"\d+$").Value)).ToArray();
      

  2.   

    string[] fileList = new string[] { "123456-asd45sdf-2", "6666-a234sdsdf-5", "7849561-asdsdf-1" };
    string[] results = fileList.OrderByDescending(x => int.Parse(Regex.Match(s, @"\d+$").Value)).ToArray();
    foreach (string result in results)
    Response.Write(result + "<br/>");
      

  3.   

     string[] fileList = new string[] { "123456-asd45sdf-2", "6666-a234sdsdf-5", "7849561-asdsdf-1" };
                fileList = fileList.OrderByDescending(t => Convert.ToInt32(Regex.Match(t, @"(?<=^(\w+\-){2})\d+").Value)).ToArray();