首页 > 学院 > 开发设计 > 正文

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

2019-11-17 02:51:28
字体:
来源:转载
供稿:网友

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

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表