Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As others have pointed out, a lexicographic sort and concatenation is close, but not quite correct. For example, for the numbers <code>5</code>, <code>54</code>, and <code>56</code> a lexicographic sort will produce <strong>{5, 54, 56}</strong> (in increasing order) or <strong>{56, 54, 5}</strong> (in decreasing order), but what we really want is <strong>{56, 5, 54}</strong>, since that produces the largest number possible.</p> <p>So we want a comparator for two numbers that somehow puts the biggest digits first.</p> <ol> <li>We can do that by comparing individual digits of the two numbers, but we have to be careful when we step off the end of one number if the other number still has remaining digits. There are lots of counters, arithmetic, and edge cases that we have to get right.</li> <li><p>A cuter solution (also mentioned by @Sarp Centel) achieves the same result as (1) but with a lot less code. The idea is to <em>compare the concatenation of two numbers with the reverse concatenation of those numbers</em>. All of the cruft that we have to explicitly handle in (1) is handled implicitly.</p> <p>For example, to compare <code>56</code> and <code>5</code>, we'd do a regular lexicographic comparison of <code>565</code> to <code>556</code>. Since <code>565</code> > <code>556</code>, we'll say that <code>56</code> is "bigger" than <code>5</code>, and should come first. Similarly, comparing <code>54</code> and <code>5</code> means we'll test <code>545</code> &lt; <code>554</code>, which tells us that <code>5</code> is "bigger" than <code>54</code>.</p></li> </ol> <p>Here's a simple example:</p> <pre><code>// C++0x: compile with g++ -std=c++0x &lt;filename&gt; #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;algorithm&gt; #include &lt;vector&gt; int main() { std::vector&lt;std::string&gt; v = { "95", "96", "9", "54", "56", "5", "55", "556", "554", "1", "2", "3" }; std::sort(v.begin(), v.end(), [](const std::string &amp;lhs, const std::string &amp;rhs) { // reverse the order of comparison to sort in descending order, // otherwise we'll get the "big" numbers at the end of the vector return rhs+lhs &lt; lhs+rhs; }); for (size_t i = 0; i &lt; v.size(); ++i) { std::cout &lt;&lt; v[i] &lt;&lt; ' '; } } </code></pre> <p>When run, this code displays:</p> <pre><code>9 96 95 56 556 5 55 554 54 3 2 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload