现在要求是这样的,对字符串排序,但是字符串含有数字。举个例子:有“第1楼、第2楼、第11楼”。现在排序是第11楼,第1楼,第2楼,而不是第1楼,第2楼,第11楼。我知道是把数字按照字符串compare的原因。可是怎么让他先按照前面的字符排序,再按照数字排序。

解决方案 »

  1.   

    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;
        }
    }