title: C++ comparison function for "version sort"?
body: <p>I see <a href="https://stackoverflow.com/questions/38544151/compare-two-string-as-numeric-value">Compare two string as numeric value</a> and I can look at the source for the <code>sort</code> command-line utility. But to my surprise, I haven't found in Boost or StackOverflow a reference implementation of a generic string comparison of what <code>man sort</code> calls <code>--version-sort</code> – sorting strings with embedded numbers. I understand there are locale issues if we want to do full numeric sort with signs, separators, and decimals, but version sort seems much easier (and addresses the common case sequential file names that aren't zero-padded). Is there a common implementation of something like this?</p>
  <p>If not,
  A naive generic implementation supporting just unsigned integers seems like it would be</p>
  <ol>
  <li>Both empty =&gt; equal.</li>
  <li>Find the first digits in each range.</li>
  <li><code>std::lexicographical_compare_three_way</code> the corresponding leading ranges.</li>
  <li>If they aren't equal, we are done.</li>
  <li>If they are equal, find the end of the digit spans.</li>
  <li>Compare the corresponding digit spans numerically.</li>
  <li>Recursively compare the remainder of the ranges.</li>
  </ol>
  <p>Where the digit-span portion is something like</p>
  <pre><code>#include &lt;algorithm&gt;
  #include &lt;cassert&gt;
  #include &lt;cctype&gt; // For std::isdigit
  #include &lt;string_view&gt;
  
  
  struct is_digit_t {
      // See https://en.cppreference.com/w/cpp/string/byte/isdigit
      [[nodiscard]] bool operator()(unsigned char c) const {
           return std::isdigit(c);
      }
      [[nodiscard]] bool operator()(const std::integral auto&amp; c) const {
          if (c != static_cast&lt;std::decay_t&lt;decltype(c)&gt;&gt;(static_cast&lt;unsigned char&gt;(c))) {
              return false; // Has to be itself as an unsigned char.
          }
          return (*this)(static_cast&lt;unsigned char&gt;(c));
      }
  };
  
  template &lt;typename It0, typename It1, typename IsDigit = is_digit_t&gt;
  std::strong_ordering compare_strings_containing_only_digits_three_way(
      It0 b0, It0 e0, 
      It1 b1, It1 e1,
      [[maybe_unused]] IsDigit is_digit = {})
  {
      assert(std::all_of(b0, e0, is_digit));
      assert(std::all_of(b1, e1, is_digit));    
      // Skip leading zeros:
      auto nonzero = [](const auto&amp; c) { return c != '0'; };
      const auto bnz0 = std::find_if_not(b0, e0, nonzero);
      const auto bnz1 = std::find_if_not(b1, e1, nonzero);
      const auto s0 = std::distance(bnz0, e0);
      const auto s1 = std::distance(bnz1, e1);
      if (s0 != s1) {
          return s0 &lt; s1 ? std::strong_ordering::less : std::strong_ordering::greater;
      }
      // Same number of digits =&gt; lexicographical compare is numerical compare:
      const auto numerical = std::lexicographical_compare_three_way(b0, e0, b1, e1);
      if (numerical != 0) {
          return numerical;
      }
      // Tiebreaker: &quot;1&quot; &lt; &quot;01&quot; &lt; &quot;001&quot; &lt; &quot;2&quot;:
      return std::lexicographical_compare_three_way(b0, bnz0, b1, bnz1);
  }
  
  template &lt;typename It0, typename It1, typename IsDigit = is_digit_t&gt;
  std::strong_ordering compare_strings_containing_unsigned_integers_three_way(
      It0 b0, It0 e0, 
      It1 b1, It1 e1,
      IsDigit is_digit = {}) 
  {
      if (b0 == e0 &amp;&amp; b1 == e1) { // Totally empty =&gt; equal.
          return std::strong_ordering::equal;
      }
      const auto numStart0 = std::find_if(b0, e0, is_digit);
      const auto numStart1 = std::find_if(b1, e1, is_digit);
  
      
      if (const auto leadingCmp = std::lexicographical_compare_three_way(b0, numStart0, b1, numStart1);
          leadingCmp != std::strong_ordering::equal) {
          return leadingCmp; // Don't have to worry about the numbers.
      }
      const auto numEnd0 = std::find_if_not(numStart0, e0, is_digit);
      const auto numEnd1 = std::find_if_not(numStart1, e1, is_digit);
      if (auto numberCmp = compare_strings_containing_only_digits_three_way(numStart0, numEnd0, numStart1, numEnd1, is_digit); 
          numberCmp != std::strong_ordering::equal) {
          return numberCmp; // Number-part tie-broke it.
      }
      // Recursively deal with everything after the first matched numbers:
      return compare_strings_containing_unsigned_integers_three_way(numEnd0, e0, numEnd1, e1);
  }
  </code></pre>
  <p><a href="https://godbolt.org/z/eEKbMj9vj" rel="nofollow noreferrer">https://godbolt.org/z/eEKbMj9vj</a></p>
  <p>So...</p>
  <ul>
  <li>Does this seem right?</li>
  <li>Is there a cleaner way?</li>
  <li>Are there really no Boost algorithms for this?</li>
  </ul>