C#中sort排序相关用法介绍

 C#中,List.Sort() 不仅为我们提供了默认的排序方法,还为我们提供了4种自定义排序的方法,通过默认排序方法,我们无需重写任何Sort()方法的实现代码,就能对单参数类型的List数据进行单一规则的排序,如果通过对这些方法进行改进我们可以轻松做到对多参数、多规则的复杂排序。

下面通过程序示例介绍四种相关的方法:

1.第一种,sort自带的list排序功能,但是该方法只是适用于单个元素的list。

using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class test 
{
    static void Main(string[] args)
    {

        List<int> list = new List<int>();
        list.Add(6);
        list.Add(4);
        list.Add(3);
        list.Add(5);
        //直接对数字进行排序
        list.Sort();
        foreach (var item in list)
        {
            Console.WriteLine(item);
        }
        Console.ReadKey();
    }
 }

对于多元素的list,需要用不同的排序方式对元素进行排序。

未排序:

using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class test 
{
    static void Main(string[] args)
    {

        List<student> stu = new List<student>();
        stu.Add(new student("apple", 23));
        stu.Add(new student("banana",18));
        stu.Add(new student("orange",19));

        foreach (var item in stu)
        {
            Console.WriteLine(item.Name+" "+" "+item.Age);
        }

        Console.ReadKey();

    }
 }

class student
{
    public student(string name, int age) { Name = name; Age = age; }
    public string Name;
    public int Age;
}



第二种接口排序:

该种方法是在定义的类内,通过Icomparable接口在类内定义排序的准则的方法。

using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class test 
{
    static void Main(string[] args)
    {

        List<student> stu = new List<student>();
        stu.Add(new student("apple", 23));
        stu.Add(new student("banana",18));
        stu.Add(new student("apple",22));
        stu.Add(new student("orange",19));

        //
        stu.Sort();
        foreach (var item in stu)
        {
            Console.WriteLine(item.Name+" "+" "+item.Age);
        }

        Console.ReadKey();
    }
 }

class student:IComparable<student>
{
    public student(string name, int age) { Name = name; Age = age; }
    public string Name;
    public int Age;

    public int CompareTo(student other)
    {
        if (this.Name!=other.Name)
        {
            return this.Name.CompareTo(other.Name);
        }
        else if (this.Age!=other.Age)
        {
            return this.Age.CompareTo(other.Age);
        }
        return 0;
    }
}

第三种方法:接口排序的第二种

该种方法时定义了一个类是作为一种方法,该类将需要排序的元素类引用,通过Icampara接口,在主函数中需要排序命令中载入该类的参数。

using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class test 
{
    static void Main(string[] args)
    {

        //第一种方法
        List<student> stu = new List<student>();
        stu.Add(new student("apple", 23));
        stu.Add(new student("banana", 18));
        stu.Add(new student("apple", 22));
        stu.Add(new student("orange", 19));

        stu.Sort();
        Console.WriteLine("第一种方法");
        foreach (var item in stu)
        {
            Console.WriteLine(item.Name + " " + " " + item.Age);
        }

        //第二种方法
        stu.Sort(new stupara());
        Console.WriteLine("第二种方法");
        foreach (var item in stu)
        {
            Console.WriteLine(item.Name + " " + " " + item.Age);
        }
    }
 }

class student:IComparable<student>
{
    public student(string name, int age) { Name = name; Age = age; }
    public string Name;
    public int Age;

    public int CompareTo(student other)
    {
        if (this.Name!=other.Name)
        {
            return this.Name.CompareTo(other.Name);
        }
        else if (this.Age!=other.Age)
        {
            return this.Age.CompareTo(other.Age);
        }
        return 0;
    }
}

class stupara : IComparer<student>
{
    public int Compare(student x, student y)
    {
        if (x.Age != y.Age)
        {
            return x.Age.CompareTo(y.Age);
        }
        else if (x.Name != y.Name)
        {
            return x.Name.CompareTo(y.Name);
        }

        else
            return 0;
    }
}

与第二种方法相比,第三种方法更好用,第三种方法可以将排序的类和方法类自由结合,无需要在定义元素类的时候将排序的方法载入。

第四种方法:通过委托类型

using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class test 
{
    static void Main(string[] args)
    {

        //通过委托类型
        List<Point> pt = new List<Point>();
        pt.Add(new Point(2, 5));
        pt.Add(new Point(4, 2));
        pt.Add(new Point(3, 4));

        Console.WriteLine("第一种委托");
        pt.Sort(delegate (Point p1, Point p2)
       {
           return p1.X.CompareTo(p2.X);
       }
       );
        foreach (var item in pt)
        {
            Console.WriteLine(item.X + " " + " " + item.Y);
        }

        //P1,P2默认为pt列表中的类型
        pt.Sort((p1, p2) =>
        {
            if (p1.Y != p2.Y)
            {
                return p1.Y.CompareTo(p2.Y);
            }
            else if (p1.X != p2.X)
            {
                return p1.Y.CompareTo(p2.Y);
            }
            else
            {
                return 0;          
            }
        });

        Console.WriteLine("第二种委托类型");
        foreach (var item in pt)
        {
            Console.WriteLine(item.X + " " + " " + item.Y);
        }

    }
 }


class Point
{
    public Point(int x, int y) { X = x;Y = y; }
    public int X;
    public int Y;
}

 

通过委托方法传入sort排序方法是一种比较简单的方式,该种方法无需重新定义类,直接在主函数中即可使用。

参考文章:

(1条消息) C# List.Sort排序_c# list sort_Peter_Gao_的博客-CSDN博客

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值