Comparer.cs 869 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // System.Collections.Comparer
  3. //
  4. // Author:
  5. // Sergey Chaban ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. namespace System.Collections {
  10. [Serializable]
  11. public sealed class Comparer : IComparer {
  12. public static readonly Comparer Default;
  13. // Class constructor
  14. static Comparer ()
  15. {
  16. Default = new Comparer ();
  17. }
  18. // Public instance constructor
  19. private Comparer ()
  20. {
  21. }
  22. // IComparer
  23. public int Compare (object a, object b)
  24. {
  25. if (a == b)
  26. return 0;
  27. else if (a == null)
  28. return -1;
  29. else if (b == null)
  30. return 1;
  31. else if (a is IComparable)
  32. return (a as IComparable).CompareTo (b);
  33. else if (b is IComparable)
  34. return -(b as IComparable).CompareTo (a);
  35. throw new ArgumentException ("Neither a nor b IComparable");
  36. }
  37. }
  38. }