📜  如何在C#中对列表进行排序List.Sort()方法集-1

📅  最后修改于: 2021-05-30 01:42:51             🧑  作者: Mango

List .Sort()方法用于使用指定的或默认的IComparer 实现或提供的Compare 委托对列表中的元素或元素的一部分进行排序,以比较列表元素。此方法的重载列表中共有4种方法,如下所示:

  1. 排序(IComparer )
  2. 排序(Int32,Int32,IComparer )
  3. 种类()
  4. 排序(比较)

在这里,我们将讨论前两种方法。

Sort(IComparer )方法

此方法用于使用指定的比较器对整个List 中的元素进行排序。

句法:

public void Sort (System.Collections.Generic.IComparer comparer);

在这里,比较器是在比较元素时使用的IComparer 实现,或者为null以使用默认比较器Default。

例外情况:

  • InvalidOperationException:如果比较器为null,并且默认比较器Default找不到类型T的IComparable 通用接口或IComparable接口的实现。
  • ArgumentException:如果比较器的实现在排序期间导致错误。例如,比较器与其自身进行比较时可能不会返回0。

范例1:

// C# program to demonstrate the concept of 
// List.Sort(IComparer ) method
using System;
using System.Collections.Generic;
  
class GFG : IComparer
{
    public int Compare(int x, int y)
    {
        if (x == 0 || y == 0)
        {
            return 0;
        }
          
        // CompareTo() method
        return x.CompareTo(y);
          
    }
}
  
public class geek
{
      
    // Main Method
    public static void Main()
    {
          
        // List initialize
        List list1 = new List
        {
              
            // list elements
            1,5,6,2,4,3
          
        };
  
        Console.WriteLine("Original List");
          
        foreach(int g in list1)
        {
              
            // Display Original List
            Console.WriteLine(g);
              
        }
          
        // "gg" is the object oif class GFG
        GFG gg = new GFG();
          
        Console.WriteLine("\nSort with a comparer:");
          
        // use of List.Sort(IComparer) 
        // method. The comparer is "gg"
        list1.Sort(gg);
          
        foreach( int g in list1 )
        {
              
            // Display sorted list
            Console.WriteLine(g);
              
        }
    }
}
输出:
Original List
1
5
6
2
4
3

Sort with a comparer:
1
2
3
4
5
6

范例2:

// C# program to demonstrate the concept of 
// List.Sort(IComparer ) method
using System;
using System.Collections.Generic;
  
class GFG : IComparer
{
    public int Compare(string x, string y)
    {
          
        if (x == null || y == null)
        {
            return 0;
        }
          
        // "CompareTo()" method
        return x.CompareTo(y);
          
    }
}
  
public class geek
{
      
    // Main Method
    public static void Main()
    {
        List list1 = new List();
          
        // list elements
        list1.Add("A");
        list1.Add("I");
        list1.Add("G");
        list1.Add("B");
        list1.Add("E");
        list1.Add("H");
        list1.Add("F");
        list1.Add("C");
        list1.Add("J");
  
        Console.WriteLine("Original List");
          
        // Display Original List
        Display(list1);
          
        // "gg" is the object
        GFG gg = new GFG();
      
        Console.WriteLine("\nSort with a comparer:");
          
        // sort the list with a 
        // specified comparer "gg"
        list1.Sort(gg);
          
        // Display sorted List
        Display(list1);
          
        Console.WriteLine("\nBinarySearch and Insert D");
  
        // Binary Search for "D"
        // using List.BinarySearch(T) method
        int index = list1.BinarySearch("D");
  
        if (index < 0)
        {
              
            // range++;
            list1.Insert(~index, "D");
              
        }
  
        // Display the List after
        // inserting "D"
        Display(list1);
          
    }
      
    // Display function
    public static void Display(List list)
    {
        foreach( string g in list )
        {
            Console.WriteLine(g);
        }
    }
}
输出:
Original List
A
I
G
B
E
H
F
C
J

Sort with a comparer:
A
B
C
E
F
G
H
I
J

BinarySearch and Insert D
A
B
C
D
E
F
G
H
I
J
List .Sort(Int32,Int32,IComparer )方法

此方法用于使用指定的比较器对List 中的一系列元素进行排序。

句法:

public void Sort(int index, int len, IComparer comparer)

参数:

例外情况:

  • ArgumentOutOfRangeException :如果indexlen小于0。
  • ArgumentException :如果index和count在List中未指定有效范围。
  • InvalidOperationException :如果比较器为null。

例子:

// C# program to demonstrate the use of 
// List.Sort(Int32, Int32, IComparer)
// Method
using System;
using System.Collections.Generic;
  
class GFG : IComparer
{
    public int Compare(string x, string y)
    {
        if (x == null || y == null)
        {
            return 0;
        }
          
        // "CompareTo()" method
        return x.CompareTo(y);
          
    }
}
  
public class geek
{
    public static void Main()
    {
        List list1 = new List();
          
        // list elements
        list1.Add("C++");
        list1.Add("Java");
        list1.Add("C");
        list1.Add("Python");
        list1.Add("HTML");
        list1.Add("CSS");
        list1.Add("Scala");
        list1.Add("Ruby");
        list1.Add("Perl");
  
        int range = 4;
        Console.WriteLine("Original List");
          
        // Display Original List
        Display(list1);
          
        // "gg" is the object
        GFG gg = new GFG();
          
        Console.WriteLine("\nSort a range with comparer:");
          
        // sort the list within a 
        // range of index 1 to 4
        // where range = 4
        list1.Sort(1, range, gg);
          
        // Display sorted List
        Display(list1);
          
        Console.WriteLine("\nBinarySearch and Insert Dart");
  
        // Binary Search and storing 
        // index value to "index"
        int index = list1.BinarySearch(0, range,
                                    "Dart", gg);
          
        if (index < 0)
        {
            list1.Insert(~index, "Dart");
            range++;
        }
  
        // Display the List 
        // after inserting "Dart"
        Display(list1);
          
    }
      
    // Display function
    public static void Display(List list)
    {
        foreach(string g in list)
        {
            Console.WriteLine(g);
        }
    }
}
输出:
Original List
C++
Java
C
Python
HTML
CSS
Scala
Ruby
Perl

Sort a range with comparer:
C++
C
HTML
Java
Python
CSS
Scala
Ruby
Perl

BinarySearch and Insert Dart
C++
C
Dart
HTML
Java
Python
CSS
Scala
Ruby
Perl

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.list-1.sort?view=netframework-4.7.2