个性化阅读
专注于IT技术分析

如何在C#中对数组排序Array.Sort()|S1

Array.Sort方法用于对一维数组中的元素进行排序。此方法的重载列表中有17种方法, 如下所示:

Sort <T>(T [])方法

Sort <T>(T [], IComparer <T>)方法

Sort <T>(T [], Int32, Int32)方法

Sort <T>(T [], 比较<T>)方法

Sort(Array, Int32, Int32, IComparer)方法

Sort(Array, Array, Int32, Int32, IComparer)方法

Sort(Array, Int32, Int32)方法

Sort(Array, Array, Int32, Int32)方法

Sort(Array, IComparer)方法

Sort(Array, Array, IComparer)方法

Sort(Array, Array)方法

Sort(Array)方法

Sort <T>(T [], Int32, Int32, IComparer <T>)方法

Sort <TKey, TValue>(TKey [], TValue [])方法

Sort <TKey, TValue>(TKey [], TValue [], IComparer <TKey>)方法

Sort <TKey, TValue>(TKey [], TValue [], Int32, Int32)方法

Sort <TKey, TValue>(TKey [], TValue [], Int32, Int32, IComparer <TKey>)方法

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

Sort <T>(T [])方法

此方法使用以下命令对数组中的元素进行排序IComparable <T>数组每个元素的通用接口实现。

语法:public static void Sort <T>(T [] array);参数:array:要排序的一维, 从零开始的Array。

例外情况:

  • ArgumentNullException:如果Array一片空白。
  • InvalidOperationException:如果数组中的一个或多个元素未实现IComparable <T>通用接口。

例子:

//C# Program to illustrate the use 
//of the Array.Sort<T>(T[]) Method
using System;
using System.Collections.Generic;
  
class GFG {
  
     //Main Method
     public static void Main()
     {
         //array elements
         string [] arr = new string [5] { "A" , "D" , "X" , "G" , "M" };
  
         foreach ( string g in arr)
         {
             Console.WriteLine(g);
             //display original array
         }
  
         Console.WriteLine( "\nAfter Sort:" );
         Array.Sort(arr);
  
         foreach ( string g in arr)
         {
             Console.WriteLine(g);
             //display sorted array
         }
  
         Console.WriteLine( "\nB sorts between :" );
  
         //binary Search for "B"
         int index = Array.BinarySearch(arr, "B" );
  
         //call "sortT" function
         //which is the Sort<T>(T[]) function
         sortT(arr, index);
          
         Console.WriteLine( "\nF sorts between :" );
         index = Array.BinarySearch(arr, "F" );
         sortT(arr, index);
     }
  
     public static void sortT<T>(T[] arr, int index)
     {
  
         //If the index is negative, //it represents the bitwise
         //complement of the next larger 
         //element in the array.
         if (index <0) {
              
             index = ~index;
  
             if (index == 0)
                 Console.Write( "beginning of array" );
             else
                 Console.Write( "{0} and " , arr[index - 1]);
  
             if (index == arr.Length)
                 Console.WriteLine( "end of array." );
             else
                 Console.WriteLine( "{0}" , arr[index]);
         }
     }
}

输出如下:

A
D
X
G
M

After Sort:
A
D
G
M
X

B sorts between :
A and D

F sorts between :
D and G

Sort <T>(T [], IComparer <T>)方法

此方法使用指定的数组对数组中的元素进行排序IComparer <T>通用接口。

语法:公共静态无效Sort <T>(T []数组, System.Collections.Generic.IComparer <T>比较器);参数:T:它是数组元素的类型。 array:这是要排序的一维数组。 comparer:比较元素时使用的是IComparer <T>通用接口实现, 或者为null以使用每个元素的IComparable <T>通用接口实现。

例外情况:

ArgumentNullException:如果数组为null。

InvalidOperationException:如果比较器为null, 并且没有实现IComparable 通用接口。

ArgumentException:

如果执行比较器在排序过程中导致错误。

例子:

//C# program to demonstrate the use of the 
//Array.Sort<T>(T[], IComparer<T>) method
using System;
using System.Collections.Generic;
  
public class GeeK : IComparer<string> {
  
     public int Compare( string x, string y)
     {
         //Compare x and y in reverse order.
         return x.CompareTo(y);
     }
}
  
class GFG {
  
     //Main Method
     public static void Main()
     {
         //array elements
         string [] arr = new string [5] { "A" , "D" , "X" , "G" , "M" };
  
         foreach ( string g in arr)
         {
  
             //display original array
             Console.WriteLine(g);
         }
  
         Console.WriteLine( "\nAfter Sort: " );
         GeeK gg = new GeeK();
  
         //Sort<T>(T[], IComparer<T>) method
         Array.Sort(arr, gg);
          
         foreach ( string g in arr)
         {
             //display sorted array
             Console.WriteLine(g);
         }
  
         Console.WriteLine( "\nD Sorts between :" );
  
         //binary Search for "D"
         int index = Array.BinarySearch(arr, "D" );
          
          //call "sortT" function
         sortT(arr, index);
         
         Console.WriteLine( "\nF Sorts between :" );
         index = Array.BinarySearch(arr, "F" );
         sortT(arr, index);
     }
  
     public static void sortT<T>(T[]arr, int index)
     {
         if (index <0)
         {
              
             //If the index is negative, //it represents the bitwise
             //complement of the next 
             //larger element in the array.
             index = ~index;
  
             Console.Write( "Not found. Sorts between: " );
  
             if (index == 0)
                 Console.Write( "Beginning of array and " );
             else
                 Console.Write( "{0} and " , arr[index-1]);
  
             if (index == arr.Length)
                 Console.WriteLine( "end of array." );
             else
                 Console.WriteLine( "{0}." , arr[index]);
         }
         else
         {
             Console.WriteLine( "Found at index {0}." , index);
         }
     }
}

输出如下:

A
D
X
G
M

After Sort: 
A
D
G
M
X

D Sorts between :
Found at index 1.

F Sorts between :
Not found. Sorts between: D and G.

Array.Sort <T>(T [], Int32, Int32)方法

此方法使用来对Array范围内的元素进行排序IComparable <T>数组每个元素的通用接口实现。

语法:public static void Sort <T>(T[]array, 整数索引, 整数长度);
参数:array:要排序的一维, 从零开始的Array。
index:它是要排序的范围的起始索引。
length:是要排序的范围内的元素数。

异常情况:

  • ArgumentNullException:如果数组为null。
  • ArgumentOutOfRangeException:如果索引小于数组的下限或长度小于零。
  • ArgumentException:如果索引和长度未在数组中指定有效范围。
  • InvalidOperationException:如果数组中的一个或多个元素未实现IComparable <T>通用接口。

例子:

//C# program to demonstrate the use of
//Array.Sort<T>(T[], Int32, Int32) method
using System;
using System.Collections.Generic;
  
public class Geek : IComparer<string> {
  
     public int Compare( string x, string y)
     {
         //Compare y and x in reverse order.
         return y.CompareTo(x);
     }
}
  
public class Example {
  
     //Main Method
     public static void Main()
     {
         //Array elements
         string [] arr = { "AB" , "CD" , "GH" , "EF" , "MN" , "IJ" };
             
         Console.WriteLine( "Original Array :" );
          
         Display(arr);
  
         Console.WriteLine( "\nSort the array between " +
                                       "index 1 to 4" );
          
         //Array.Sort(T[], Int32, Int32) method
         //sort will happen in between
         //index 1 to 4
         Array.Sort(arr, 1, 4);
         Display(arr);
  
         Console.WriteLine( "\nSort the array reversely" +
                            " in between index 1 to 4" );
          
         //sort will happen in between
         //index 1 to 4 reversely          
         Array.Sort(arr, 1, 4, new Geek());
      
         Display(arr);
     }
  
     public static void Display( string [] arr)
     {
         foreach ( string g in arr)
         {
             Console.WriteLine(g);
         }
     }
}

输出如下:

Original Array :
AB
CD
GH
EF
MN
IJ

Sort the array between index 1 to 4
AB
CD
EF
GH
MN
IJ

Sort the array reversely in between index 1 to 4
AB
MN
GH
EF
CD
IJ

Array.Sort <T>(T [], 比较<T>)方法

此方法使用指定的Comparison <T>对数组中的元素进行排序。

语法:public static void Sort <T>(T []数组, Compare <T>比较);参数:array:要排序的一维基于零的数组。比较:比较元素时使用的compare <T>。

例外情况:

  • ArgumentNullException:如果数组为null或比较为null。
  • ArgumentException:如果执行比较在排序过程中导致错误。

例子:

//C# program to demonstrate the use of the 
//Array.Sort<T>(T[ ], Comparison<T>) Method
using System;
using System.Collections.Generic;
  
class GFG {
      
     private static int CompareComp( string x, string y)
     {
         if (y == null && x == null ) {
              
             //If x and y is null
             //then x and y are same
             return 0;
         }
         else {
              
             //If x is null but y is not 
             //null then y is greater.
             return -1;
         }
     }
  
     //Main method
     public static void Main()
     {
         string [] arr = { "Java" , "C++" , "Scala" , "C" , "Ruby" , "Python" };
        
         Console.WriteLine( "Original Array: " );
          
         //display original array
         Display(arr);
          
         Console.WriteLine( "\nSort with Comparison: " );
  
         //Array.Sort<T>(T[], Comparison<T>)
         //Method
         Array.Sort(arr, CompareComp);
          
         //display sorted array
         Display(arr);
          
     }
  
     //Display function
     public static void Display( string [] arr)
     {
         foreach ( string g in arr)
         {
             Console.WriteLine(g);
         }
     }
}

输出如下:

Original Array: 
Java
C++
Scala
C
Ruby
Python

Sort with Comparison: 
Python
Ruby
C
Scala
C++
Java

参考:

  • https://docs.microsoft.com/en-us/dotnet/api/system.array.sort?view=netframework-4.7.2

赞(0)
未经允许不得转载:srcmini » 如何在C#中对数组排序Array.Sort()|S1

评论 抢沙发

评论前必须登录!