Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Display the list according to the length then by name in ascending order

C# Sharp LINQ : Exercise-28 with Solution

Write a program in C# Sharp to display the list of items in the array according to the length of the string then by name in ascending order.

Sample Solution:-

C# Sharp Code:

using System;
using System.Linq;
using System.Collections.Generic;
 
class  LinqExercise28
{
	static void Main(string[] args)
		{

         string[] cities =  
            {  
                "ROME","LONDON","NAIROBI","CALIFORNIA","ZURICH","NEW DELHI","AMSTERDAM","ABU DHABI", "PARIS"  
            };  
  
            Console.Write("\nLINQ : Display the list according to the length then by name in ascending order : "); 
            Console.Write("\n--------------------------------------------------------------------------------\n");	
            Console.Write("\nThe cities are : 'ROME','LONDON','NAIROBI','CALIFORNIA','ZURICH','NEW DELHI','AMSTERDAM','ABU DHABI','PARIS' \n");	            
			Console.Write("\nHere is the arranged list :\n");
			IEnumerable<string> cityOrder =
			cities.OrderBy(str => str.Length)
                            .ThenBy(str => str);
			foreach (string city in cityOrder)
				Console.WriteLine(city);    
				Console.ReadLine();
		}
}

Sample Output:

LINQ : Display the list according to the length then by name in ascending order :                             
--------------------------------------------------------------------------------                              
The cities are : 'ROME','LONDON','NAIROBI','CALIFORNIA','ZURICH','NEW DELHI','AMSTERDAM','ABU DHABI','PARIS'  
                                                                                                       
Here is the arranged list :                                                                                   
ROME                                                                                                       
PARIS                                                                                                       
LONDON                                                                                                       
ZURICH                                                                                                       
NAIROBI                                                                                                       
ABU DHABI                                                                                                     
AMSTERDAM                                                                                                     
NEW DELHI                                                                                                     
CALIFORNIA

Pictorial Presentation:

C# Sharp LINQ: Display the list according to the length then by name in ascending order.

Flowchart:

Flowchart: LINQ : Display the list according to the length then by name in ascending order

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to generate a Right Outer Join between two data sets.
Next: Write a program in C# Sharp to split a collection of strings into some groups.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.