A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

C# program to sort a list of integers using the Linq OrderBy() method
Q:

C# program to sort a list of integers using the Linq OrderBy() method

0

C# program to sort a list of integers using the Linq OrderBy() method

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to sort a list of integers using the Linq OrderBy() method, is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# Program to sort a list of integers 
//using Linq OrderBy() method

using System;
using System.Linq;
using System.Collections.Generic;

class Demo
{
    static void Main(string[] args)
    {
        List<int> list = new List<int>() { 49,34,13,56,25,65 };
        
        var result = list.OrderBy(num=>num);

        Console.WriteLine("Sorted list in Ascending order:");
        foreach (int value in result)
        {
            Console.Write(value + " ");
        }
        Console.WriteLine();
    }
}

Output:

Sorted list in Ascending order:
13 25 34 49 56 65
Press any key to continue . . .

Explanation:

In the above program, we created a class Demo that contains the Main() method. In the Main() method we created a list that contains unordered integer numbers.

var result = list.OrderBy(num=>num);

The above method call will return the sorted list in the ascending order.

Console.WriteLine("Sorted list in Ascending order:");
foreach (int value in result)
{
    Console.Write(value + " ");
}
Console.WriteLine();

In the above code, we accessed each integer number one by one and print it on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now