title: Sorting bounded rectangles in opencv
body: <p>I have a set of bounded rectangles as Rect in a vector.</p>
  
  <pre><code>vector(Rect) boundRect( contours.size() );
  </code></pre>
  
  <p>I want to sort these rectangles like in the image below</p>
  
  <p><a href="http://img42.com/liVFt" rel="nofollow noreferrer">image http://img42.com/liVFt</a></p>
  
  <p>I have already tried using the method below, but I am not getting the order like in the image I have posted.</p>
  
  <pre><code>stable_sort( boundRect.begin(), boundRect.end(), compareX_rect );
  stable_sort( boundRect.begin(), boundRect.end(), compareY_rect );
  
  bool compareX_rect(const Rect &amp; a, const Rect &amp;b) {
      return a.x &gt;= b.x;
  }
  bool compareY_rect(const Rect &amp; a, const Rect &amp;b) {
      return a.y &gt;= b.y;
  }
  </code></pre>
  
  <p>Can someone please help me with this? Thanks in advance.</p>