1. 程式人生 > >字串按數字排序

字串按數字排序

功能:按字串中的數字進行排序。

先對字串去重,然後排序使用正則表示式。

using System.Text.RegularExpressions; //引用

string[] stringArray = { "a11", "b1", "a2", "a2","a5","b2","b1"};
            //List用於儲存從數組裡取出來的不相同的元素
            List<string> listString = new List<string>();
            foreach (string eachString in listband)
            {
                if
(!listString.Contains(eachString)) listString.Add(eachString); } //按數字大小排序 使用正則提取陣列中每個值中的數字 根據數字大小進行升序排列 listString = listString.OrderBy(s => int.Parse(Regex.Match(s, @"\d+").Value)).ToList();

結果為:按數字大小排序