title: Working of sort() in c++ while sorting strings
body: <p>I was solving a problem, <a href="https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/baaki-che/" rel="nofollow noreferrer">QuestionsLink</a>, where we are given a name along with an integer. You have to print three names with the maximum integer values.</p>
  
  <p>So, I converted the number to a string and concatenated it before the names. After sorting I was getting desired results. But it failed some test cases which made me curious about how the sort function in c++ works.</p>
  
  <p>Here is my code:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;bits/stdc++.h&gt;
  #define ll long long int
  
  using namespace std;
  
  int main() {
  
    ll n;
    cin &gt;&gt; n;
  
    string names[n];
    int arr[n], digits[n];
  
    for (int i = 0; i &lt; n; i++) {
      cin &gt;&gt; names[i] &gt;&gt; arr[i];
      names[i] = to_string(arr[i]) + names[i];
    }
  
    for (int i = 0; i &lt; n; i++) {
      digits[i] = floor(log10(arr[i]) + 1);
    }
  
    sort(names, names + n, greater&lt;string&gt;());
  
    for (int i = 0; i &lt; n; i++) {
      cout &lt;&lt; names[i] &lt;&lt; endl;
    }
    /*for(int i=0;i&lt;3;i++){
       string s = names[i].substr(digits[i],names[i].length());
       cout&lt;&lt;s&lt;&lt;endl;
     }
     cout&lt;&lt;digits[98];
   */
    return 0;
  }
  </code></pre>
  
  <p>Test cases:</p>
  
  <pre><code>9huDmy
  995YnAYoAEE
  990RUTM
  96ipORo
  956LNjFa 
  </code></pre>
  
  <p>So, After sorting in descending order how is 9huDmy on top while the next two are sorted based on the digits in front of them. </p>