排序种种用法与相比较

By admin in 4858.com on 2019年4月9日

 

 

上面介绍种种List的sort的用法与相比

C#排序种种用法与相比较。 List 排序种种用法与相比,

 

下边介绍各类List的sort的用法与比较

率先,大家建多个People的实体,有name、age、sex的质量,大家要排序的字段是年龄age

新建三个实体类

    public class People
    {
        public string name { get; set; }
        public int age { get; set; }
        public string sex { get; set; }
    }

新建list的数据

            List<People> peoples = new List<People>()
            {
                new People() {age = 11, name="alun", sex = "男"},
                new People() {age=25, name = "陈敬桃", sex = "男"},
                new People() {age=9, name = "惠安", sex = "男"},
                new People() {age = 45, name = "小票", sex = "女"},
                new People() {age=3, name = "晓鸥", sex = "女"},
                new People() {age=70, name = "望谟", sex = "男"}
            };

 

1. 第3种排序方法,使用 IComparer

    public class PeopleAgeComparer : IComparer<People>
    {
        public int Compare(People p1, People p2)
        {
            return p1.age.CompareTo(p2.age);
        }
    }

peoples.Sort(new PeopleAgeComparer());

能够看出第贰种艺术比价麻烦,要新建多少个新的类来做

 

 

2. 第二种排序方法,使用委托来排序

peoples.Sort(delegate (People p1, People p2) { return p1.age.CompareTo(p2.age); });

看委托的方法很便宜,不用新建类这么忙绿。

 

 

3. 第三种排序方法,使用Lambda表明式来排序

peoples.Sort( (a, b) => a.age.CompareTo(b.age) );

 

 

 

目测排序有这3种方法,个人觉得还是Lambda表达式使用得方便。

List 排序各类用法与相比较,
上边介绍各类List的sort的用法与比较首先,大家建一个People的实业,有name、age、sex的个性,我们要排序的字段…

上边介绍各类List的sort的用法与相比

下边介绍各类List的sort的用法与相比

先是,大家建二个People的实体,有name、age、sex的属性,我们要排序的字段是年龄age

首先,大家建四个People的实体,有name、age、sex的属性,大家要排序的字段是年龄age

率先,我们建贰个People的实业,有name、age、sex的属性,大家要排序的字段是年龄age

新建三个实体类

新建八个实体类

新建多少个实体类

1  public class People
2     {
3         public string name { get; set; }
4         public int age { get; set; }
5         public string sex { get; set; }
6     }
    public class People
    {
        public string name { get; set; }
        public int age { get; set; }
        public string sex { get; set; }
    }
    public class People
    {
        public string name { get; set; }
        public int age { get; set; }
        public string sex { get; set; }
    }

新建list的数据

新建list的数据

新建list的数据

 List<People> peoples = new List<People>()
            {
                new People() {age = 11, name="alun", sex = "男"},
                new People() {age=25, name = "陈敬桃", sex = "男"},
                new People() {age=9, name = "惠安", sex = "男"},
                new People() {age = 45, name = "小票", sex = "女"},
                new People() {age=3, name = "晓鸥", sex = "女"},
                new People() {age=70, name = "望谟", sex = "男"}
            };
            List<People> peoples = new List<People>()
            {
                new People() {age = 11, name="alun", sex = "男"},
                new People() {age=25, name = "陈敬桃", sex = "男"},
                new People() {age=9, name = "惠安", sex = "男"},
                new People() {age = 45, name = "小票", sex = "女"},
                new People() {age=3, name = "晓鸥", sex = "女"},
                new People() {age=70, name = "望谟", sex = "男"}
            };
            List<People> peoples = new List<People>()
            {
                new People() {age = 11, name="alun", sex = "男"},
                new People() {age=25, name = "陈敬桃", sex = "男"},
                new People() {age=9, name = "惠安", sex = "男"},
                new People() {age = 45, name = "小票", sex = "女"},
                new People() {age=3, name = "晓鸥", sex = "女"},
                new People() {age=70, name = "望谟", sex = "男"}
            };

1. 第3种排序方法,使用 IComparer

 

 

public class PeopleAgeComparer : IComparer<People>
    {
        public int Compare(People p1, People p2)
        {
            return p1.age.CompareTo(p2.age);
        }
    }

peoples.Sort(new PeopleAgeComparer());

1. 第二种排序方法,使用 IComparer

1. 第一种排序方法,使用 IComparer

能够看来第二种办法比价麻烦,要新建八个新的类来做

    public class PeopleAgeComparer : IComparer<People>
    {
        public int Compare(People p1, People p2)
        {
            return p1.age.CompareTo(p2.age);
        }
    }

peoples.Sort(new PeopleAgeComparer());
    public class PeopleAgeComparer : IComparer<People>
    {
        public int Compare(People p1, People p2)
        {
            return p1.age.CompareTo(p2.age);
        }
    }

peoples.Sort(new PeopleAgeComparer());

 

能够见到第②种办法比价麻烦,要新建四个新的类来做

可以看看第二种办法比价麻烦,要新建3个新的类来做

2. 第一种排序方法,使用委托来排序

 

 

peoples.Sort(delegate (People p1, People p2) { return p1.age.CompareTo(p2.age); });

 

 

看委托的点子很方便,不用新建类这么费力。

2. 第三种排序方法,使用委托来排序

2. 第壹种排序方法,使用委托来排序

 

peoples.Sort(delegate (People p1, People p2) { return p1.age.CompareTo(p2.age); });
peoples.Sort(delegate (People p1, People p2) { return p1.age.CompareTo(p2.age); });

3. 第二种排序方法,使用Lambda表明式来排序

看委托的点子很有益,不用新建类这么勤奋。

看委托的章程很便宜,不用新建类这么劳碌。

 1
peoples.Sort( (a, b) => a.age.CompareTo(b.age) ); 

 

 

 

 

 

个人感觉:方法一就算有个别麻烦然则简单接纳,方法二不容易明白不太简单选拔,方法三用到lambda表明式就算很粗大略可是用的时候不必然能想的到,有供给需求死记

3. 第三种排序方法,使用Lambda表明式来排序

3. 第三种排序方法,使用Lambda表明式来排序

 

peoples.Sort( (a, b) => a.age.CompareTo(b.age) );
peoples.Sort( (a, b) => a.age.CompareTo(b.age) );

 

 

 

FROM:

 

 

 

 

4858.com,目测排序有那3种艺术,个人认为依然Lambda表明式使用得便宜。

目测排序有那三种艺术,个人认为如故Lambda表明式使用得便宜。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有