I have a std::map<std::string, std::string>.
It performs fine, but there is a slight issue with sorting: it sorts entries like this:```
val1
val10
val11
...
val2
val20
val21
...
val3
However I want it to sort them in normal order:```
val1
val2
...
val9
val10
val11
val12
...
val19
val20
```I assume I need to write my own `std::less` definition for it? Or is there an existing class for this?
My current solution is somewhat... messy```cpp
struct NaturalOrder {
bool operator() (
const std::string& a,
const std::string& b
) const noexcept {
std::size_t i = 0;
std::size_t j = 0;
while (i < a.size() && j < b.size()) {
// If both are digits, compare the numeric values
if (std::isdigit(a[i]) && std::isdigit(b[j])) {
std::size_t start_i = i;
std::size_t start_j = j;
// Skip leading zeros for fair comparison
while (i < a.size() && a[i] == '0') ++i;
while (j < b.size() && b[j] == '0') ++j;
// Parse the numeric values
while (i < a.size() && std::isdigit(a[i])) ++i;
while (j < b.size() && std::isdigit(b[j])) ++j;
// Compare the lengths of the numeric parts
std::size_t len_a = i - start_i, len_b = j - start_j;
if (len_a != len_b) return len_a < len_b;
// Compare the numeric values digit by digit
int cmp = a.substr(start_i, len_a).compare(b.substr(start_j, len_b));
if (cmp != 0) return cmp < 0;
} else {
// If not digits, compare character by character
if (a[i] != b[j]) return a[i] < b[j];
++i;
++j;
}
}
// If one string has remaining characters, it is considered greater
return a.size() < b.size();
}
}

