欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

如何排序含有数字的字符串

程序员文章站 2022-04-22 20:20:35
...

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;

    }

}

 

FROM:https://blog.csdn.net/fwj380891124/article/details/39693673