title: How to implement IComparable<Time> in my Struct?
body: <p>My code includes struct called <code>Time</code>.</p>
  
  <p>I would like to implement <code>IComparable&lt;Time&gt;</code> interface in my struct to compare two variables of type 'Time'.</p>
  
  <p>I've already did it with <code>IEquatable</code> and it works, but I can't figure out how to do it with <code>IComparable</code>.</p>
  
  <p>Since <code>Time</code> consists of three variables (hours, min, sec) I can't simply consider each case in <code>if</code>. Or can I? Could you give me a hint how to code it? Thank you.</p>
  
  <pre class="lang-cs prettyprint-override"><code>{    
      public struct Time : IEquatable&lt;Time&gt;, IComparable&lt;Time&gt;
      {
          private byte hours, minutes, seconds;
  
          public byte Hours { get { return hours; } }
          public byte Minutes { get { return minutes; } }
          public byte Seconds { get { return seconds; } }
  
          public Time(byte hh, byte mm, byte ss)
          {
              if (hh &lt; 0 || hh &gt; 24)
                  throw new ArgumentOutOfRangeException();
              if (mm &lt; 0 || mm &gt; 60)
                  throw new ArgumentOutOfRangeException();
              if (ss &lt; 0 || ss &gt; 60)
                  throw new ArgumentOutOfRangeException();
  
              this.hours = hh;
              this.minutes = mm;
              this.seconds = ss;
          }
  
          public Time(byte hh, byte mm) : this(hh, mm, default(byte)) { }
  
          public Time(byte hh) : this(hh, default(byte), default(byte)) { }
  
          public Time(string hms)
          {
              string[] arr = hms.Split(':');
  
              this.hours = Convert.ToByte(arr[0]);
              this.minutes = Convert.ToByte(arr[1]);
              this.seconds = Convert.ToByte(arr[2]);
          }
  
          public override string ToString()
          {
              return Hours + ":" + Minutes + ":" + Seconds;
          }
  
          public override bool Equals(object t)
          {
              if (t == null || this.GetType() != t.GetType()) return false;
  
              return (this.Hours == ((Time)t).Hours &amp;&amp;
                  this.Minutes == ((Time)t).Minutes &amp;&amp;
                  this.Seconds == ((Time)t).Seconds);
          }
  
          public override int GetHashCode()
          {
              return this.GetHashCode();
          }
  
          // IEquatable
          public bool Equals(Time other)
          {
              return this.Equals((object)other);
          }
  
          public static bool operator == (Time t1, Time t2)
          {
              return t1.Equals(t2);
          }
  
          public static bool operator !=(Time t1, Time t2)
          {
              return !t1.Equals(t2);
          }
  
          // IComparable
          public int CompareTo(Time other)
          {
              if (this == other)
                  return 0;
  
              // Code that compares two variables
          }
  
          public static bool operator &gt; (Time t1, Time t2)
          {
              return t1.CompareTo(t2) == 1;
          }
  
          public static bool operator &lt; (Time t1, Time t2)
          {
              return t1.CompareTo(t2) == -1;
          }
  
          public static bool operator &gt;= (Time t1, Time t2)
          {
              return t1.CompareTo(t2) &gt;= 0;
          }
  
          public static bool operator &lt;= (Time t1, Time t2)
          {
              return t1.CompareTo(t2) &lt;= 0;
          }
      }
  }
  </code></pre>