How to: Sort Arrays
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

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.


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

© 2016 Microsoft