当前位置: 移动技术网 > IT编程>开发语言>c# > C#基础教程之IComparable用法,实现List<T>.sort()排序

C#基础教程之IComparable用法,实现List<T>.sort()排序

2019年07月18日  | 移动技术网IT编程  | 我要评论

 list<t>.sort()可以实现对t的排序,比如list<int>.sort()执行后集合会按照int从小到大排序。如果t是一个自定义的object,可是我们想按照自己的方式来排序,那该怎么办呢,其实可以用过icomparable接口重写compareto方法来实现。流程如下:

      一.第一步我们申明一个类person但是要继承icomparable接口:

复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
namespace testicomparable
{
    public class person : icomparable<person>
    {
        public string name { get; set; }
        public int age { get; set; }
        public int compareto(person obj)
        {
            int result;
            if (this.name == obj.name && this.age == obj.age)
            {
                result = 0;
            }
            else
            {
                if (this.name.compareto(obj.name) > 0)
                {
                    result = 1;
                }
                else if (this.name == obj.name && this.age > obj.age)
                {
                    result = 1;
                }
                else
                {
                    result = -1;
                }
            }
            return result;
        }
        public override string tostring()
        {
            return this.name + "-" + this.age;
        }
    }
}

  二.然后在主函数里面调用sort方法即可.类就会按照姓名从小到大,如果姓名相同则按照年龄从小到大排序了。

复制代码 代码如下:

public class program
{
    public static void main(string[] args)
    {
        list<person> lstperson = new list<person>();
        lstperson.add(new person(){ name="bob",age=19});
        lstperson.add(new person(){ name="mary",age=18});
        lstperson.add(new person() { name = "mary", age = 17 });
        lstperson.add(new person(){ name="lily",age=20});
        lstperson.sort();
        console.readkey();
    }
}

   三,如果不继承icomparable接口,我们该如何实现排序呢。可以使用linq来实现。其实效果是一样的,只是如果类的集合要经常排序的话,建议使用继承接口的方法,这样可以简化sort的代码,而且更容易让人看懂。

复制代码 代码如下:

public static void main(string[] args)
        {
            list<person> lstperson = new list<person>();
            lstperson.add(new person(){ name="bob",age=19});
            lstperson.add(new person(){ name="mary",age=18});
            lstperson.add(new person() { name = "mary", age = 17 });
            lstperson.add(new person(){ name="lily",age=20});
            lstperson.sort((x,y) =>
            {
                int result;
                if (x.name == y.name && x.age == y.age)
                {
                    result = 0;
                }
                else
                {
                    if (x.name.compareto(y.name) > 0)
                    {
                        result = 1;
                    }
                    else if (x.name == y.name && x.age > y.age)
                    {
                        result = 1;
                    }
                    else
                    {
                        result = -1;
                    }
                }
                return result;
            });
            console.readkey();
        }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网