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 string names using the Linq OrderBy() method
Q:

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

0

C# program to sort a list of string names 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 string names 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 string names 
//using the Linq OrderBy() method.

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

class Demo
{
    static void Main(string[] args)
    {
        List<string> list = new List<string>() {"Andy", "Gren", "Steve","Arun","Brock"};
        
        var result = list.OrderBy(name=>name);

        Console.WriteLine("Names in sorted order:");
        foreach (string name in result)
        {
            Console.Write(name + " ");
        }
        Console.WriteLine();
    }
}

Output:

Names in sorted order:
Andy Arun Brock Gren Steve
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 string names.

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

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

Console.WriteLine("Names in sorted order:");
foreach (string name in result)
{
    Console.Write(name + " ");
}
Console.WriteLine();

In the above code, we accessed each name 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