title: Sorting a list of vectors lexicographically according to priorities
body: <p>Say I have a list of vectors of strings:</p>
  
  <p>["a", "c", "duck"]</p>
  
  <p>["a", "a", "f"]</p>
  
  <p>["bee", "s", "xy"]</p>
  
  <p>["b", "a", "a"]</p>
  
  <p>I want to sort the vectors in this way:</p>
  
  <p>first sorting lexicographically with respect to the element at index 0, and if there's a tie, it will be determined lexicographically with respect to the element at index 1, and if there's another tie, it will be determined lexicographically with respect to the element at index 2.</p>
  
  <p>So the list above will be as follows after being sorted:</p>
  
  <p>["a", "a", "f"]</p>
  
  <p>["a", "c", "duck"]</p>
  
  <p>["b", "a", "a"]</p>
  
  <p>["bee", "s", "xy"]</p>
  
  <p>How can I implement the standard library sort() function to write a method to sort a list of vectors according to the above description? I'm using C++.
  Thanks. </p>
  
  <p>It's not hard to write the compare function once the length of each vector is known. But what if I don't know the length of the vectors (but I always know they are of the same length)?
  Compare function for vectors of length 3:</p>
  
  <pre><code>bool CompareVector(vector&lt;string&gt;  first, vector&lt;string&gt;  second){
      if (first[0] &lt; second[0])
         return true;
      if (first[1] &lt; second[1])
         return true;
      if (first[2] &lt; second[2])
         return true;
      return false;
  
  }
  </code></pre>
  
  <p>So for vectors of length n, there will be n if statements. But how can I keep the number of if statements a variable?</p>
  
  <p>How about this:</p>
  
  <pre><code> bool CompareVector(vector&lt;string&gt;  first, vector&lt;string&gt;  second){
      for (int i=0; i&lt; first.size(); i++)
         if (first[i] &lt; second[i])
           return true;
      return false;
  </code></pre>
  
  <p>}</p>
  
  <p>Then I can call the standard sort function:</p>
  
  <pre><code>sort(vector&lt;vector&lt;string&gt; &gt;input.begin(), vector&lt;vector&lt;string&gt; &gt;input.end(), CompareVector() )
  </code></pre>
  
  <p>Would this work? Thanks.</p>