Advertisement
nina75

Multidimensional Arrays/Task5

Dec 29th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.71 KB | None | 0 0
  1. //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).
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. class SortingArrays
  7. {
  8.     static void Main()
  9.     {
  10.         string[] testArray = { "mouse", "crocodile", "cat", "goat", "monkey", "snail" };
  11.         Array.Sort(testArray, new StringsComparer());
  12.         foreach (string str in testArray)
  13.         {
  14.             Console.Write(str + " ");
  15.         }
  16.         Console.WriteLine();
  17.     }
  18. }
  19.  
  20. class StringsComparer : IComparer<string>
  21. {
  22.     public int Compare(string string1, string string2)
  23.     {
  24.         return string1.Length.CompareTo(string2.Length);
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement