This post will discuss how to sort an integer array in C#.

1. Using Array.Sort Method

The recommended method to in-place sort an integer array is with Array.Sort method. It is overloaded to accept custom comparers, and default behavior is to sort the array in ascending order. For example,

Download  Run Code

2. Using Enumerable.OrderBy Method

To avoid any modification to the original array, we can create a sorted copy of it. The idea is to use the LINQ’s Enumerable.OrderBy method to get a sorted sequence using the default comparer. Then, we can use the ToArray() method to get an array.

Download  Run Code

That’s all about sorting an integer array in C#.