fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. List<BaseCats> cats = new List<BaseCats>
  9. {
  10. new BaseCats("Ману", 5),
  11. new BaseCats("Марго", 10),
  12. new BaseCats("Васян", 7)
  13. };
  14. cats.Sort();
  15. Console.WriteLine(String.Join(",", cats));
  16.  
  17. }
  18. }
  19.  
  20. class BaseCats : IComparable {
  21. private readonly int _Age;
  22.  
  23. public BaseCats(string name, int age) {
  24. _Age = age;
  25. }
  26.  
  27. public override string ToString() {
  28. return ""+ _Age;
  29. }
  30.  
  31. public int CompareTo(object obj)
  32. {
  33. BaseCats bCats = obj as BaseCats;
  34. if (bCats != null)
  35. {
  36. if (this._Age < bCats._Age)
  37. {
  38. return -1;
  39. }
  40. else if (this._Age > bCats._Age)
  41. {
  42. return 1;
  43. }
  44. else
  45. {
  46. return 0;
  47. }
  48.  
  49. }
  50. else
  51. {
  52. throw new Exception("Параметр должен быть типа BaseCats");
  53. }
  54. }
  55. }
Success #stdin #stdout 0.01s 15012KB
stdin
Standard input is empty
stdout
5,7,10