数値を含む文字列のソート:巧みな方法

2440 ワード

using System; using System.Collections.Generic; class Program { static void Main(string[] args) { string[] floors ={ " 3 ", " 2 ", " 11 " }; Array.Sort<string>(floors, Factory.Comparer); foreach (string s in floors) Console.WriteLine(s); Console.ReadKey(); } } //  
class Factory : IComparer<string> { private Factory() { } public static IComparer<string> Comparer { get { return new Factory(); } } public int Compare(string x, string y) { return x.Length == y.Length ? x.CompareTo(y) : x.Length - y.Length; } }