fork download
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. List<Person> list = new List<Person>()
  10. {
  11. new Person("Adrian", "Abramowicz"),
  12. new Person("Krzysiek", "Abramowicz"),
  13. new Person("Krzysiek", "Kowalski"),
  14. new Person("Marian", "Zawada")
  15. };
  16.  
  17. BubbleSort(list);
  18.  
  19. foreach(Person p in list)
  20. Console.WriteLine(p.Surname + " " + p.Name);
  21.  
  22. }
  23.  
  24. public static void BubbleSort(List<Person> list)
  25. {
  26. for (int i = 0; i < list.Count; i++)
  27. {
  28. for (int j = 0; j < list.Count - 1; j++)
  29. {
  30. if (list[j] > list[j+1])
  31. {
  32. Person temp = list[j];
  33. list[j] = list[j+1];
  34. list[j+1] = temp;
  35. }
  36. }
  37. }
  38. }
  39. }
  40.  
  41. public class Person : IComparable, IComparable<Person>
  42. {
  43. public Person(string name, string surname)
  44. {
  45. Name = name;
  46. Surname = surname;
  47. }
  48.  
  49. public string Name {get; private set;}
  50. public string Surname {get; private set;}
  51.  
  52. public override int GetHashCode()
  53. {
  54. return Name.GetHashCode() ^ Surname.GetHashCode();
  55. }
  56.  
  57. public override bool Equals(Object obj)
  58. {
  59. Person other = obj as Person;
  60.  
  61. return Equals(other);
  62. }
  63.  
  64. public bool Equals(Person other)
  65. {
  66. if (object.ReferenceEquals(other, null))
  67. return false;
  68.  
  69. if (!Surname.Equals(other.Surname))
  70. return false;
  71.  
  72. if (!Name.Equals(other.Name))
  73. return false;
  74.  
  75. return true;
  76. }
  77.  
  78. public int CompareTo(object obj)
  79. {
  80. Person other = obj as Person;
  81.  
  82. return CompareTo(other);
  83. }
  84.  
  85. public int CompareTo(Person other)
  86. {
  87. if (object.ReferenceEquals(other, null))
  88. return 1;
  89.  
  90. int result = string.Compare(this.Surname, other.Surname, StringComparison.OrdinalIgnoreCase);
  91.  
  92. if (result == 0)
  93. result = string.Compare(this.Name, other.Name, StringComparison.OrdinalIgnoreCase);
  94.  
  95. return result;
  96. }
  97.  
  98. public static int Compare(Person left, Person right)
  99. {
  100. if (object.ReferenceEquals(left, right))
  101. return 0;
  102.  
  103. if (object.ReferenceEquals(left, null))
  104. return -1;
  105.  
  106. int result = string.Compare(left.Surname, right.Surname, StringComparison.OrdinalIgnoreCase);
  107.  
  108. if (result == 0)
  109. result = string.Compare(left.Name, right.Name, StringComparison.OrdinalIgnoreCase);
  110.  
  111. return result;
  112. }
  113.  
  114. public static bool operator < (Person left, Person right)
  115. {
  116. return (Compare(left, right) < 0);
  117. }
  118.  
  119. public static bool operator > (Person left, Person right)
  120. {
  121. return (Compare(left, right) > 0);
  122. }
  123. }
Success #stdin #stdout 0.02s 33912KB
stdin
Standard input is empty
stdout
Abramowicz Adrian
Abramowicz Krzysiek
Kowalski Krzysiek
Zawada Marian