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

简介: 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排序方法是一种比较简单的方式,该种方法无需重新定义类,直接在主函数中即可使用。


相关文章
|
24天前
|
安全 C#
C# List基本用法
C# List基本用法
|
24天前
|
C#
C# Dev chartControl的用法
C# Dev chartControl的用法
|
2月前
|
安全 编译器 C#
C#学习相关系列之多线程---lock线程锁的用法
C#学习相关系列之多线程---lock线程锁的用法
|
2月前
|
C#
C#学习相关系列之多线程---ConfigureAwait的用法
C#学习相关系列之多线程---ConfigureAwait的用法
|
2月前
|
C#
C#学习相关系列之多线程---TaskCompletionSource用法(八)
C#学习相关系列之多线程---TaskCompletionSource用法(八)
|
2月前
|
Java C#
C#学习系列相关之多线程(五)----线程池ThreadPool用法
C#学习系列相关之多线程(五)----线程池ThreadPool用法
|
4月前
|
开发框架 .NET 编译器
C# 9.0中的静态匿名函数:引入static关键字的新用法
【1月更文挑战第15天】C# 9.0为匿名函数带来了一个新的修饰符static,允许开发者明确指定匿名函数不会捕获其包含作用域中的任何变量。这一特性增强了代码的性能和可读性,同时减少了因不小心捕获变量而导致的潜在错误。本文将详细探讨C# 9.0中静态匿名函数的语法、使用场景以及它们如何影响代码的性能和安全性。
|
2月前
|
开发框架 .NET C#
C#学习相关系列之Linq用法---where和select用法(二)
C#学习相关系列之Linq用法---where和select用法(二)
|
2月前
|
安全 C#
c#学习相关系列之as和is的相关用法
c#学习相关系列之as和is的相关用法
|
2月前
|
C# 索引
C#学习相关系列之abstract和virtual用法
C#学习相关系列之abstract和virtual用法