字符串数据按照大小排序

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        string[] filenames = new string[] { "1-1 课程入门", "1-10 课程练习", "1-11 课程答案", "1-2 课程讲解", "1-24 课程讲解" };
        var result = filenames.OrderBy(x => PadNumbers(x));    
       foreach (string r in result)
       {
           Response.Write(r+"<br>");
       } 
    }


    public static string PadNumbers(string input){
    return Regex.Replace(input, "[0-9]+", match => match.Value.PadLeft(10, '0'));}

 

}

 

 

在那里PadNumbers可以定义为:

public static string PadNumbers(string input){
return Regex.Replace(input, "[0-9]+", match => match.Value.PadLeft(10, '0'));}
这会将输入字符串中出现的任何数字(或多个数字)填充零,这样可以OrderBy看到:

ABC0000000010
ABC0000000001...AB0000000011

 

结果:

1-1 课程入门
1-2 课程讲解
1-10 课程练习
1-11 课程答案
1-24 课程讲解

 

posted @ 2020-06-19 09:39  启明星工作室  阅读(223)  评论(0编辑  收藏  举报