Advertisement
lina94

StringSortingMethod

Jul 23rd, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. // 5. You are given an array of strings. Write a method that sorts the array by the length of its elements
  5. // (the number of characters composing them).
  6.  
  7. class StringSortingMethod
  8. {
  9.     static void Main()
  10.     {
  11.         //FOR USER INPUT UNCOMMENT THE FOLLOWING CODE:
  12.  
  13.         //Console.WriteLine("How many words do you want to sort?");
  14.         //int n = int.Parse(Console.ReadLine());
  15.         //string[] stringArray = new string[n];
  16.  
  17.         //for (int i = 0; i < stringArray.Length; i++)
  18.         //{
  19.         //    stringArray[i] = Console.ReadLine();
  20.         //}
  21.  
  22.         string[] stringArray = {"Maiden", "Metallica", "Accept", "Whitesnake", "INXS", "Purple", "Depeche", "WASP", "Judas", "Queen", "Scorpions", "AC/DC", "Aerosmith", "Rainbow", "Sabbath", "Motorhead","Helloween", "KISS"};
  23.  
  24.         // sorting using LINQ
  25.         var sortedArray = stringArray.OrderBy(strings => strings.Length);
  26.  
  27.         // printing sorted array
  28.         Console.WriteLine("Array sorted by length of its elements:");
  29.         foreach (var stringItem in sortedArray)
  30.         {
  31.             Console.WriteLine(stringItem);
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement