Condividi tramite


How to: Sort Arrays

Unlike standard C++ arrays, managed arrays are implicitly derived from an array base-class from which they inherit common behaviors. A prime example is the Sort method, which can be used to order the items in any array.

For arrays containing simple intrinsic types, you can call the Sort method. The sort criteria can be overridden, which is necessary to sort for arrays of complex types. In this case, the array element type must implement the IComparable::CompareTo method.

Example

// array_sort.cpp
// compile with: /clr
using namespace System;

int main() {
   array<int>^ a = { 5, 4, 1, 3, 2 };
   Array::Sort( a );
   for (int i=0; i < a->Length; i++)
      Console::Write("{0} ", a[i] );
}

See Also

Tasks

How to: Sort Arrays Using Custom Criteria

Reference

Array