title: Multi-property sort is inverting elements
body: <p>I am trying to implement a multiple-property sort on some data and approached the problem by doing back-to-back <code>stable_sorts</code>:</p>
  
  <pre><code>bool DecreasingA(const Data &amp; a, const Data &amp; b) {
      return a.A &gt;= b.A;
  }
  
  bool DecreasingB(const Data &amp; a, const Data &amp; b) {
      if (a.B)
          return true;  // if a.B is true, maintain ordering
      if (b.B)
          return false; // a.B is false, but b.B is true, to swap elements
      return true;      // a.B and b.B are false, maintain ordering
  }
  
  std::vector&lt;Data&gt; myVector;
  
  // add Data to vector here
  
  std::stable_sort(myVector.begin(), myVector.end(), DecreasingA());
  std::stable_sort(myVector.begin(), myVector.end(), DecreasingB());
  </code></pre>
  
  <p>But after I do this, <code>myVector</code> has the A property in the opposite order that I want. Even though it was correctly sorted after the first <code>stable_sort</code></p>
  
  <p>For Example:</p>
  
  <p>Before the sorting:</p>
  
  <pre><code>A B C
  
  1 0 5
  1 1 8
  2 0 2
  0 1 3
  </code></pre>
  
  <p>After first sort (Decreasing A) looks good:</p>
  
  <pre><code>A B C
  
  2 0 2
  1 0 5
  1 1 8
  0 1 3
  </code></pre>
  
  <p>After second sort (Decreasing B) I get this:</p>
  
  <pre><code>A B C
  
  0 1 3
  1 1 8
  1 0 5
  2 0 2
  </code></pre>
  
  <p>Instead of what I want:</p>
  
  <pre><code>A B C
  
  1 1 8
  0 1 3
  2 0 2
  1 0 5
  </code></pre>
  
  <p>I am thinking it is an issue with my <code>DecreasingB</code> function, but I have tried all sorts of logic and keep getting this inversion. Any suggestions?</p>
  
  <p>I know I could write one compare function to handle this particular situation, but I need flexibility for the end user to select which properties to sort and in what order they are applied. </p>