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

方法描述

基于第一个 Array 中的关键字,使用每个关键字的 IComparable 实现,对两个一维 Array 对象(一个包含关键字,另一个包含对应的项)的部分元素进行排序。

语法定义(C# System.Array.Sort 方法 (Array, Array, Int32, Int32) 的用法)

public static void Sort(
	Array keys,
	Array items,
	int index,
	int length
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
keys System-Array 一维 Array,它包含要排序的关键字。
items System-Array 一维 Array,其中包含与 keysArray 中每个键对应的项。- 或 -null 则只对 keysArray 进行排序。
index System-Int32 排序范围的起始索引。
length System-Int32 排序范围内的元素数。
返回值 void

提示和注释

keys Array 中的每个关键字在 itemsArray 中都有对应项。 当关键字在排序过程中重新定位时,itemsArray 中的对应项也同样重新定位。 因此,itemsArray 根据 keysArray 中对应关键字的排列进行排序。

keys Array 中指定元素范围内的每个关键字均必须实现 IComparable 接口,才能与其他任何关键字进行比较。

如果项数多于键数,您仍可以进行排序,但没有对应的键的项将不会被排序。 如果键数多于项数,您不能进行排序,这样做会引发 ArgumentException。

如果排序不能成功地完成,则结果未定义。

此方法使用 QuickSort 算法。 此实现执行不稳定排序;亦即,如果两元素相等,则其顺序可能不被保留。 相反,稳定排序则会保持相等元素的顺序。

一般情况下,此方法的运算复杂度为 O(n log n),其中 n 是 length;最坏的情况下其运算复杂度为 O(n ^ 2)。

System.Array.Sort 方法 (Array, Array, Int32, Int32)例子

请注意,结果可能因当前 CultureInfo 而异。

using System;
using System.Collections;

public class SamplesArray  {

   public class myReverserClass : IComparer  {

      // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
      int IComparer.Compare( Object x, Object y )  {
          return( (new CaseInsensitiveComparer()).Compare( y, x ) );
      }

   }

   public static void Main()  {

      // Creates and initializes a new Array and a new custom comparer.
      String[] myKeys = { "red", "GREEN", "YELLOW", "BLUE", "purple", "black", "orange" };
      String[] myValues = { "strawberries", "PEARS", "LIMES", "BERRIES", "grapes", "olives", "cantaloupe" };
      IComparer myComparer = new myReverserClass();

      // Displays the values of the Array.
      Console.WriteLine( "The Array initially contains the following values:" );
      PrintKeysAndValues( myKeys, myValues );

      // Sorts a section of the Array using the default comparer.
      Array.Sort( myKeys, myValues, 1, 3 );
      Console.WriteLine( "After sorting a section of the Array using the default comparer:" );
      PrintKeysAndValues( myKeys, myValues );

      // Sorts a section of the Array using the reverse case-insensitive comparer.
      Array.Sort( myKeys, myValues, 1, 3, myComparer );
      Console.WriteLine( "After sorting a section of the Array using the reverse case-insensitive comparer:" );
      PrintKeysAndValues( myKeys, myValues );

      // Sorts the entire Array using the default comparer.
      Array.Sort( myKeys, myValues );
      Console.WriteLine( "After sorting the entire Array using the default comparer:" );
      PrintKeysAndValues( myKeys, myValues );

      // Sorts the entire Array using the reverse case-insensitive comparer.
      Array.Sort( myKeys, myValues, myComparer );
      Console.WriteLine( "After sorting the entire Array using the reverse case-insensitive comparer:" );
      PrintKeysAndValues( myKeys, myValues );

   }

   public static void PrintKeysAndValues( String[] myKeys, String[] myValues )  {
      for ( int i = 0; i < myKeys.Length; i++ )  {
         Console.WriteLine( "   {0,-10}: {1}", myKeys[i], myValues[i] );
      }
      Console.WriteLine();
   }
}


/* 
This code produces the following output.

The Array initially contains the following values:
   red       : strawberries
   GREEN     : PEARS
   YELLOW    : LIMES
   BLUE      : BERRIES
   purple    : grapes
   black     : olives
   orange    : cantaloupe

After sorting a section of the Array using the default comparer:
   red       : strawberries
   BLUE      : BERRIES
   GREEN     : PEARS
   YELLOW    : LIMES
   purple    : grapes
   black     : olives
   orange    : cantaloupe

After sorting a section of the Array using the reverse case-insensitive comparer:
   red       : strawberries
   YELLOW    : LIMES
   GREEN     : PEARS
   BLUE      : BERRIES
   purple    : grapes
   black     : olives
   orange    : cantaloupe

After sorting the entire Array using the default comparer:
   black     : olives
   BLUE      : BERRIES
   GREEN     : PEARS
   orange    : cantaloupe
   purple    : grapes
   red       : strawberries
   YELLOW    : LIMES

After sorting the entire Array using the reverse case-insensitive comparer:
   YELLOW    : LIMES
   red       : strawberries
   purple    : grapes
   orange    : cantaloupe
   GREEN     : PEARS
   BLUE      : BERRIES
   black     : olives

*/

异常

异常 异常描述
ArgumentNullException keys 为 null。
RankException
  • keys Array 是多维的。
  • items Array 是多维的。
ArgumentOutOfRangeException
  • index 小于 keys 的下限。
  • length 小于零。
ArgumentException
  • items 不是 null,并且 keys 的长度大于 items 的长度。
  • index 和 length 未指定 keysArray 中的有效范围。
  • items 不为 null,并且 index 和 length 未在 itemsArray 中指定有效范围。
InvalidOperationException keys Array 中的一个或多个元素未实现 IComparable 接口。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。