How do you sort an array in C# in ascending order?

In C#, you can sort an array in ascending order using the built-in Array.Sort() method. This method modifies the original array directly and arranges elements from smallest to largest value.

Syntax

Following is the syntax for sorting an array in ascending order −

Array.Sort(arrayName);

Parameters

  • arrayName − The array to be sorted. This parameter is required and the array is modified in-place.

Using Array.Sort() Method

The Array.Sort() method automatically sorts elements in ascending order. Here's how to sort an integer array −

using System;

namespace Demo {
   public class MyApplication {
      public static void Main(string[] args) {
         int[] list = {98, 23, 97, 36, 77};
         Console.WriteLine("Original Unsorted List:");
         foreach (int i in list) {
            Console.Write(i + " ");
         }
         
         Array.Sort(list);
         
         Console.WriteLine("\nSorted List:");
         for(int i = 0; i 

The output of the above code is −

Original Unsorted List:
98 23 97 36 77 
Sorted List:
23 36 77 97 98 

Sorting Different Data Types

The Array.Sort() method works with various data types including strings, characters, and floating-point numbers −

using System;

class Program {
   public static void Main() {
      // Sorting string array
      string[] names = {"John", "Alice", "Bob", "Diana"};
      Console.WriteLine("Original Names:");
      Console.WriteLine(string.Join(", ", names));
      
      Array.Sort(names);
      Console.WriteLine("Sorted Names:");
      Console.WriteLine(string.Join(", ", names));
      
      // Sorting double array
      double[] scores = {85.5, 92.3, 78.9, 95.1};
      Console.WriteLine("\nOriginal Scores:");
      Console.WriteLine(string.Join(", ", scores));
      
      Array.Sort(scores);
      Console.WriteLine("Sorted Scores:");
      Console.WriteLine(string.Join(", ", scores));
   }
}

The output of the above code is −

Original Names:
John, Alice, Bob, Diana
Sorted Names:
Alice, Bob, Diana, John

Original Scores:
85.5, 92.3, 78.9, 95.1
Sorted Scores:
78.9, 85.5, 92.3, 95.1

Using Array.Sort() with Custom Range

You can also sort a specific portion of an array by specifying the starting index and length −

using System;

class Program {
   public static void Main() {
      int[] numbers = {50, 30, 80, 10, 70, 40};
      Console.WriteLine("Original Array:");
      Console.WriteLine(string.Join(" ", numbers));
      
      // Sort only elements from index 1 to 4 (length of 3)
      Array.Sort(numbers, 1, 3);
      
      Console.WriteLine("Partially Sorted Array (index 1-3):");
      Console.WriteLine(string.Join(" ", numbers));
   }
}

The output of the above code is −

Original Array:
50 30 80 10 70 40
Partially Sorted Array (index 1-3):
50 10 30 80 70 40

Conclusion

The Array.Sort() method in C# provides an efficient way to sort arrays in ascending order. It works with various data types and modifies the original array in-place. You can sort entire arrays or specific ranges as needed.

Updated on: 2026-03-17T07:04:35+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements