Advertisement
cap7ainjack

Problem 7. * Generic Array Sort ?

Sep 25th, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5.  
  6. public class Program
  7. {
  8. public static void Main()
  9. {
  10. int[] nums = {1, 2, 3, 4, 5, 1, 0 , 5};
  11. string[] words = {"zaz", "cba", "baa", "azis"};
  12. DateTime[] dates =
  13. {
  14. new DateTime(2002,3,1), new DateTime(2015,5,6), new DateTime(2014,1,1)
  15. };
  16.  
  17. List<int> numList = new List<int>(new int[] { 1, 2, 3, 4 ,5, 1, 0, 5} );
  18. List<string> stringList = new List<string>(new string[] { "zaz", "cba", "baa", "azis"} );
  19. List<DateTime> dateList = new List<DateTime>(new DateTime[] {
  20. new DateTime(2002,3,1), new DateTime(2015,5,6), new DateTime(2014,1,1)
  21. } );
  22.  
  23. SortArray<int>(numList);
  24. SortArray<string>(stringList);
  25. SortArray<DateTime>(dateList);
  26.  
  27. }
  28.  
  29. static void SortArray<T>(IList<T> coll) where T : IComparable
  30. {
  31. bool areSorted = false;
  32. do
  33. {
  34. areSorted = true;
  35. for (int i = 0; i < coll.Count - 1; i++)
  36. {
  37. if (coll[i].CompareTo(coll[i+1]) > 0)
  38. {
  39. var exchangeValue = coll[i];
  40. coll[i] = coll[i + 1];
  41. coll[i + 1] = exchangeValue;
  42. areSorted = false;
  43. }
  44. }
  45. } while (areSorted == false);
  46.  
  47.  
  48. Console.WriteLine(string.Join(",",coll));
  49.  
  50. }
  51.  
  52.  
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement