Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- // 5. You are given an array of strings. Write a method that sorts the array by the length of its elements
- // (the number of characters composing them).
- class StringSortingMethod
- {
- static void Main()
- {
- //FOR USER INPUT UNCOMMENT THE FOLLOWING CODE:
- //Console.WriteLine("How many words do you want to sort?");
- //int n = int.Parse(Console.ReadLine());
- //string[] stringArray = new string[n];
- //for (int i = 0; i < stringArray.Length; i++)
- //{
- // stringArray[i] = Console.ReadLine();
- //}
- string[] stringArray = {"Maiden", "Metallica", "Accept", "Whitesnake", "INXS", "Purple", "Depeche", "WASP", "Judas", "Queen", "Scorpions", "AC/DC", "Aerosmith", "Rainbow", "Sabbath", "Motorhead","Helloween", "KISS"};
- // sorting using LINQ
- var sortedArray = stringArray.OrderBy(strings => strings.Length);
- // printing sorted array
- Console.WriteLine("Array sorted by length of its elements:");
- foreach (var stringItem in sortedArray)
- {
- Console.WriteLine(stringItem);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement