排序含有数字的字符串:一个巧妙地方法

时间:2022-05-03
本文章向大家介绍排序含有数字的字符串:一个巧妙地方法,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
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;
    }
}