字符串数组按照大小排序

有时候我们会需要把获取到的路径数组,字符串数组或其他类型的字符串数组排序,大家可能会遍历来单独排序,下面介绍一下使用正则表达式来排序。

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 讲解2" };
        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 讲解2


转自:https://www.cnblogs.com/mqingqing123/p/13161395.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值