simonses

ArraySortByLengthOfElements

Jan 24th, 2013
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class SortArrayByLength
  6. {
  7. static void Main()
  8. {
  9. // Initialize a List of strings.
  10. List<string> array = new List<string>{ "11", "1", "1133", "144441", "4311", "76572211" };
  11.  
  12. // Send the List to the method.
  13. foreach (string s in SortByLength(array))
  14. {
  15. Console.WriteLine(s);
  16. }
  17. }
  18.  
  19. static IEnumerable<string> SortByLength(IEnumerable<string> e)
  20. {
  21. // Use LINQ to sort the array received and return a copy.
  22. var sorted = from s in e
  23. orderby s.Length ascending
  24. select s;
  25. return sorted;
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment