C# program to sort an integer array using the predefined method

Here, we are going to learn how to sort an integer array using the predefined method in C#?
Submitted by Nidhi, on September 03, 2020

Here we will create an array of integer elements then we sort the elements of the array in ascending order using the Sort() method of Array class.

Program:

The source code to sort an integer array using the predefine method is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# Program to sort an array using the 
//predefined method.

using System;

class Sample
{
    public static void Main()
    {
        int[] intArr = new int[5]{10,30,40,20,50};
        int loop = 0;

        Array.Sort(intArr);
        
        Console.WriteLine("Array after Sort() method: ");
        for (loop = 0; loop < intArr.Length; loop++)
        {
            Console.WriteLine(intArr[loop]);
        }
    }
}

Output:

Array after Sort() method:
10
20
30
40
50
Press any key to continue . . .

Explanation:

In the above program, we created a Sample class that contains the Main() method. In the Main() method, we created an array of integers that contains 5 elements.

Array.Sort(intArr);

Here we sorted the elements of array "intArr" in the ascending order using the Sort() method of Array class and then print the sorted array on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.