Genericsの要素をComparisonで並べ替え
Comparisonには-1,0,1の3つを返す関数を登録する
List<FileInfo> files = new List<FileInfo>(); foreach (FileInfo file in dir.GetFiles("*.jpg")) { files.Add(file); } Comparison<FileInfo> comp = new Comparison<FileInfo>(delegate(FileInfo a, FileInfo b) { int an = extractInt(a.Name); int bn = extractInt(b.Name); if (an > bn) return 1; else if (an == bn) return 0; else return -1; }); files.Sort(comp);
正規表現が苦手
public static int extractInt(String str) { String num = ""; bool numFounded = false; foreach (char c in str.ToCharArray()) { if (Regex.IsMatch(new String(c, 1), "[0-9]")) { num += c; numFounded = true; } else if (numFounded) // 文字→数字→文字と来た時 { break; } } try { return int.Parse(num); } catch { return -1; } }