Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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).
- using System;
- using System.Collections.Generic;
- class SortingArrays
- {
- static void Main()
- {
- string[] testArray = { "mouse", "crocodile", "cat", "goat", "monkey", "snail" };
- Array.Sort(testArray, new StringsComparer());
- foreach (string str in testArray)
- {
- Console.Write(str + " ");
- }
- Console.WriteLine();
- }
- }
- class StringsComparer : IComparer<string>
- {
- public int Compare(string string1, string string2)
- {
- return string1.Length.CompareTo(string2.Length);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement