Advertisement
tvarbanov

CSharp2-02-05

Dec 24th, 2013
78
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.  
  3. class StringSort
  4. {
  5.     static void StrSort(string[] strArray)
  6.     {
  7.         //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).
  8.  
  9.         //Get the lenght of the string array
  10.         int length = strArray.Length;
  11.         //New array to store the string's lenght
  12.         int[] lenghts = new int[length];
  13.  
  14.         //Strores the lenght of every string in the second array
  15.         for (int index = 0; index < length; index++)
  16.         {
  17.             lenghts[index] = strArray[index].Length;
  18.         }
  19.  
  20.         //Sorts both arrays as a pair - sorts the first array and rearange the items in the second in the same way
  21.         Array.Sort(lenghts, strArray);
  22.  
  23.         //Print the sorted array
  24.         Console.WriteLine("***** Sorted Array *****");
  25.         for (int index = 0; index < length; index++)
  26.         {
  27.             Console.WriteLine(strArray[index]);
  28.         }
  29.     }
  30.  
  31.     static void Main()
  32.     {
  33.         string[] strings = { "asxz", "sdfdss", "assdfsdfffxz", "ax", "asz", "asxasz", "aszzzzzzzzzzzzzzzxz" };
  34.  
  35.         StrSort(strings);
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement