title: C++ Sort a vector of pointers in which the objects represent matrix coordinates
body: <p>I read a file in which a matrix is filled with 0, 1 and 2. 
  When I find a 1 I create a BlueCar, when 2 I create RedCar:</p>
  
  <pre><code>class BlueCar : public Car
  {
      public:
          BlueCar(){};
          BlueCar(int x, int y);
          void move();
          virtual ~BlueCar();
  };
  </code></pre>
  
  <hr>
  
  <pre><code>class RedCar : public Car
  {
      public:
          RedCar(){};
          RedCar(int x, int y);
          void move();
          virtual ~RedCar();
  };
  </code></pre>
  
  <hr>
  
  <pre><code>class Car
  {
      public:
          Car();
          Car(int x, int y);
          virtual ~Car();
          virtual void move() = 0;
  
      private:
          int x,y;
  };
  </code></pre>
  
  <p>With this objects I fill two vectors:</p>
  
  <pre><code>    std::vector&lt;BluCar*&gt; *sparseBlu;
      std::vector&lt;RedCar*&gt; *sparseRed;
  </code></pre>
  
  <p>Considering that I need to move the cars of the matrix, and that Blue ones move downward and Red ones move rightward, I think the best approach is to sort this vectors. In that way I can see quickly if the position next to the car I'm considering is empty.</p>
  
  <p>Since Blue cars move downward I think it's better to sort "sparseBlu" first by column and then by row, instead "sparseRed" first by row and then by column.</p>
  
  <p>How can I achieve this result?
  It's better (in terms of performance) to sort the vector immediately when I fill it car by car, right?</p>