Techiehook Techiehook
Updated date May 06, 2024
In this article, we will learn how to sort an array of integers in C# using the Array.Sort() method.

How to Sort an Array in C#?

In C#, you can sort an array using the Array.Sort() method for arrays of primitive types or arrays of types that implement the IComparable interface.

using System;

class Program
    static void Main(string[] args)
        // Sample array of integers
        int[] numbers = { 5, 2, 9, 1, 6 };

        // Sorting the array

        // Displaying the sorted array
        Console.WriteLine("Sorted Numbers:");
        foreach (int num in numbers)
            Console.Write(num + " ");

We first declare an array of integers and then use Array.Sort() to sort the array in ascending order.


Sorted Numbers:
1 2 5 6 9



Admin, Australia

Welcome to! We are all about tech, lifestyle, and more. As the site admin, I share articles on programming, tech trends, daily life, and reviews... For more detailed information, please check out the user profile

Comments (0)

There are no comments. Be the first to comment!!!