public class MyComparer : System.Collections.IComparer { public MyComparer() {
}
#region IComparer メンバ
public int Compare(object x, object y) { string a = (string)x; string b = (string)y;
//正規表現で末尾の数字を取得 System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("(?<num>\\d*)$"); int numA = int.Parse(regex.Match(a).Groups["num"].Value); int numB = int.Parse(regex.Match(b).Groups["num"].Value);
return numA - numB; }
#endregion }
という風にIComparerの実装を作っておいて
string[] ary = new string[] { "ABC012", "ZZ101", "Z99", "A23", "B11" }; Array.Sort(ary, new MyComparer());