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


相关文章
|
安全 编译器 C#
C#学习相关系列之多线程---lock线程锁的用法
C#学习相关系列之多线程---lock线程锁的用法
663 1
|
安全 C#
C# List基本用法
C# List基本用法
C#学习相关系列之多线程---ConfigureAwait的用法
C#学习相关系列之多线程---ConfigureAwait的用法
1024 0
C#学习相关系列之多线程---TaskCompletionSource用法(八)
C#学习相关系列之多线程---TaskCompletionSource用法(八)
1117 0
|
Java C#
C#学习系列相关之多线程(五)----线程池ThreadPool用法
C#学习系列相关之多线程(五)----线程池ThreadPool用法
1374 0
|
开发框架 .NET C#
C#学习相关系列之Linq用法---where和select用法(二)
C#学习相关系列之Linq用法---where和select用法(二)
1653 2
C# Hashtable的用法
哈希表(HashTable)是一种通过键值对直接访问的数据结构。Add 方法用于添加成员,先检查成员是否已存在,若不存在则计算其 ASCII 码值作为散列值并添加到表中。Remove 方法用于移除成员,Size 方法返回集合成员数量。代码实现了这些功能,确保集合操作的高效性。
|
C#
c#中switch case语句的用法
C#中的 `switch case`语句提供了一种简洁而高效的方式来处理多个条件分支。通过了解其基本语法、注意事项和高级用法,可以在实际开发中灵活运用 `switch case`,提高代码的可读性和维护性。希望本文能帮助你更好地理解和使用C#中的 `switch case`语句。
1671 0