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
        Array.Sort(numbers);

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

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

Output:

Sorted Numbers:
1 2 5 6 9

 

ABOUT THE AUTHOR

Techiehook
Techiehook
Admin, Australia

Welcome to TechieHook.com! 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

https://www.techiehook.com/profile/alagu-mano-sabari-m

Comments (0)

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