如何使用IComparable在C#中排序 [英] How to use IComparable to sort in c#

查看:124
本文介绍了如何使用IComparable在C#中排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个字符串列表
例如:
行李箱= 2 * 12
行李箱= 2 * 1
行李箱= 2 * 2

我想在C#中通过IComparable对它进行排序.

Hi
I have a list of string
for example :
Trunk=2*12
Trunk=2*1
Trunk=2*2

I want sort it by IComparable in c# .

How to do it ?

推荐答案

字符串已经支持IComparable,只需对它进行如下排序:
String already supports IComparable, just sort it like this:
using System;

public class Program
{
    public static void Main()
    {
        string[] strArray = new string[]
        {
           "Trunk=2*12", "Trunk=2*1", "Trunk=2*2"
        };

        Array.Sort(strArray);
        foreach (string s in strArray)
        {
            Console.WriteLine(s);
        }
    }
}


阅读更多:如何在C#中使用IComparable和IComparer接口 [


Read more: How to use the IComparable and IComparer interfaces in C#[^]


这篇关于如何使用IComparable在C#中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆