title: List.Sort alleging that my IComparer is inconsistent
body: <p>I have a list of tblDetail objects. tblDetails have txtTracked, a string. I want to sort based on this property. so I do:</p>
  
  <pre><code>switch (sortby)
  {
      //...
      case "Tracked Y/N":
          list.Sort((lhs, rhs) =&gt; {
              return lhs.txtTracked.CompareTo(rhs.txtTracked);
          });
          break;
      case "Tracked Y/N-desc":
          list.Sort((lhs, rhs) =&gt; {
              return -lhs.txtTracked.CompareTo(rhs.txtTracked);
          });
          break;
      //...
  }
  </code></pre>
  
  <p>except sometimes txtTracked is null. Ok, fine, so I do: </p>
  
  <pre><code>switch (sortby)
  {
      //...
      case "Tracked Y/N":
          list.Sort((lhs, rhs) =&gt; {
              string toCompare = lhs.txtTracked;
              if (toCompare == null)
                  toCompare = "";
              return toCompare.CompareTo(rhs.txtTracked);
          });
          break;
      case "Tracked Y/N-desc":
          list.Sort((lhs, rhs) =&gt; {
              string toCompare = lhs.txtTracked;
              if (toCompare == null)
                  toCompare = "";
              return -toCompare.CompareTo(rhs.txtTracked);
          });
          break;
      //...
  }
  </code></pre>
  
  <p>The regular ascending sort works fine, but for some reason when I do this with the descending sort on the same set of data, I get a null reference exception on lhs in "Tracked Y/N-desc". so I do: </p>
  
  <pre><code>switch (sortby)
  {
      //...
      case "Tracked Y/N":
          list.Sort((lhs, rhs) =&gt; {
              string toCompare = lhs.txtTracked;
              if (toCompare == null)
                  toCompare = "";
              return toCompare.CompareTo(rhs.txtTracked);
          });
          break;
      case "Tracked Y/N-desc":
          //for some reason this sort adds null into the list. wat.
          list.Sort((lhs, rhs) =&gt; {
              if (lhs == null)
                  return 1;
              if (rhs == null)
                  return -1;
              string toCompare = lhs.txtTracked;
              if (toCompare == null)
                  toCompare = "";
              return -toCompare.CompareTo(rhs.txtTracked);
          });
          list.RemoveAll(d =&gt; d == null);
          break;
      //...
  }
  </code></pre>
  
  <p>only now I get a new error:</p>
  
  <pre><code>Unable to sort because the IComparer.Compare() method returns inconsistent results. Either a value does not compare equal to itself, or one value repeatedly compared to another value yields different results.
  </code></pre>
  
  <p>what gives? It works fine sorting one way but not the other. How does C# decide that my sort function isn't good enough, and how can I make it right?</p>
  
  <p>and as an aside, why is null getting added into my list when I call sort?</p>