zoukankan      html  css  js  c++  java
  • 字符串数据按照大小排序

    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 课程讲解
  • 相关阅读:
    linux
    算法
    算法
    数据结构 与 算法
    mysql
    mysql
    mysql
    mysql
    【解决】Could not get JDBC Connection、java.lang.InterruptedException问题和排查过程
    git: unable to checkout working tree error: unable to create file Filename too long on windows
  • 原文地址:https://www.cnblogs.com/mqingqing123/p/13161395.html
Copyright © 2011-2022 走看看