How to sort an array in C#?



The following is the integer array.

int[] arr = { 99, 43, 86 };

To sort, use the Sort() method.

Array.Sort(arr);

The following is the complete code displaying how to sort an array in C# using the Sort() method.

Example

 Live Demo

using System;
class Demo {
   static void Main() {
      int[] arr = { 99, 43, 86 };
      // sort
      Array.Sort(arr);
      Console.WriteLine("Sorted List");
      foreach (int res in arr) {
         Console.Write("
"+res);       }       Console.WriteLine();    } }

Output

Sorted List
43
86
99

Advertisements