码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-09-29 20:00:12      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   sp   div   c   

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

 

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

标签:style   blog   color   io   ar   for   sp   div   c   

原文地址:http://www.cnblogs.com/smiler/p/4000662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!