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

📅  最后修改于: 2021-05-29 22:32:44             🧑  作者: Mango

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

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

在这里,在Set – 1中讨论了前两种方法。因此,我们将讨论最后两种方法。

Sort()方法

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

范例1:

// C# program to demonstrate the 
// use of List.Sort() method
using System;
using System.Collections.Generic;
  
class GFG {
  
    // 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);
        }
  
        Console.WriteLine("\nSorted List");
  
        // use of List.Sort() method
        list1.Sort();
  
        foreach(int g in list1)
        {
  
            // Display sorted list
            Console.WriteLine(g);
        }
    }
}
输出:
Original List
1
5
6
2
4
3

Sorted List
1
2
3
4
5
6

范例2:

// C# program to demonstrate the 
// use of List.Sort() method
using System;
using System.Collections.Generic;
  
class GFG {
  
    // 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("D");
  
        Console.WriteLine("Original List");
  
        // Display Original List
        Display(list1);
  
        Console.WriteLine("\nSorted List");
  
        // use of List.Sort() method
        list1.Sort();
  
        // Display sorted List
        Display(list1);
    }
  
    // Display function
    public static void Display(List list)
    {
        foreach(string g in list)
        {
            Console.Write(g + " ");
        }
    }
}
输出:
Original List
A I G B E H F C D 
Sorted List
A B C D E F G H I

范例3:

// C# program to demonstrate the
// use of List.Sort() method
using System;
using System.Collections.Generic;
  
public class GFG {
  
    // Main Method
    public static void Main()
    {
        // array elements
        String[] list = {"C++", "Java", "C",
                    "Python", "HTML", "CSS",
                    "Scala", "Ruby", "Perl"};
  
        var list1 = new List();
    
        // "AddRange" method to add the
        // string array elements into the List
        list1.AddRange(list);
          
  
        Console.WriteLine("List in unsorted order: ");
        Display(list1);
  
        Console.WriteLine(Environment.NewLine);
  
        // using List.Sort() method
        list1.Sort();
          
        Console.WriteLine("List in sorted order: ");
        Display(list1);
    }
  
    // Display method
    static void Display(List list)
    {
        foreach(string g in list)
        {
            Console.Write(g + "\t");
        }
    }
}
输出:

List in unsorted order: 
C++    Java    C    Python    HTML    CSS    Scala    Ruby    Perl    

List in sorted order: 
C    C++    CSS    HTML    Java    Perl    Python    Ruby    Scala
Sort(IComparer )方法

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

例外情况:

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

下面的程序说明了上述方法的用法:

范例1:

// C# program to demonstrate the use of 
// List.Sort(comparison ) method
using System;
using System.Collections.Generic;
  
class GFG {
  
    private static int Geek(string x,
                            string y)
    {
        if (x == null) {
  
            if (y == null) {
  
                // If x and y is null
                // then they are equal
                return 0;
            }
  
            else {
  
                // If x is null but y is not
                // null then y is greater.
                return -1;
            }
        }
  
        else {
  
            if (y == null) {
  
                return 1;
            }
  
            else {
  
                // If the strings are of equal length,
                // sort them with string comparison.
                return x.CompareTo(y);
            }
        }
    }
   
    // Main Method
    public static void Main()
    {
        List list1 = new List();
  
        // list elemetns
        list1.Add("AB");
        list1.Add("CD");
        list1.Add("GH");
        list1.Add("EF");
        list1.Add("IJ");
        list1.Add("KL");
        Console.WriteLine("Original List :");
  
        // displaying original list
        Display(list1);
         
  
        Console.WriteLine("\nSort with generic Comparison object :");
  
        // Sort(Comparison) method
        //"Geek" is Comparison object
        list1.Sort(Geek);
          
        // displaying sorted list
        Display(list1);
         
    }
  
    // display function
    private static void Display(List list)
    {
        foreach(string g in list)
        {
            Console.WriteLine(g);
        }
    }
}
输出:
Original List :
AB
CD
GH
EF
IJ
KL

Sort with generic Comparison object :
AB
CD
EF
GH
IJ
KL

范例2:

// C# program to demonstrate the use of
// List.Sort(comparison ) method
using System;
using System.Collections.Generic;
  
class GFG {
  
    private static int Geek(int x, int y)
    {
        if (x == 0) {
  
            if (y == 0) {
  
                // If x and y is null
                // then they are equal
                return 0;
            }
  
            else {
  
                // If x is null but y is not 
                // null then y is greater.
                return -1;
            }
        }
  
        else {
  
            if (y == 0) {
  
                return 1;
            }
  
            else {
  
                // If the strings are of equal length,
                // sort them with string comparison.
                return x.CompareTo(y);
            }
        }
    }
  
    public static void Main()
    {
        List list1 = new List();
  
        // list elemetns
        list1.Add(2);
        list1.Add(5);
        list1.Add(6);
        list1.Add(4);
        list1.Add(1);
        list1.Add(3);
  
        Console.WriteLine("Original List :");
  
        // displaying original list
        Display(list1);
          
        Console.WriteLine("\nSort with generic "+
                          "Comparison object :");
  
         // Sort(Comparison) method
        //"Geek" is Comparison object
        list1.Sort(Geek);
         
        // displaying sorted list
        Display(list1);     
    }
  
    // display function
    private static void Display(List list)
    {
        foreach(int g in list)
        {
            Console.WriteLine(g);
        }
    }
}
输出:
Original List :
2
5
6
4
1
3

Sort with generic Comparison object :
1
2
3
4
5
6

参考:

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