Вот минимальный и потенциально недостаточный (не обрабатывает лидирующие нули, пробелы и т. Д.) Пример, который делает то, что вам нужно.
Комментарии объясняют, что он делает. :)
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> strings = {
"-1", "-1", "-20", "-4", "3", "0", "-0", "1", "20", "20", "44020",
};
// Assumes everything in "strings" has no whitespace in it.
// Assumes everything in "strings" does not have leading zeroes.
// Assumes everything in "strings" is an ascii representaion of an integer.
// Assumes everything in "strings" is nonempty.
std::sort(strings.begin(), strings.end(),
[](const std::string &a, const std::string &b) {
const bool a_is_negative = a[0] == '-';
const bool b_is_negative = b[0] == '-';
if (a_is_negative != b_is_negative) {
// If they have different signs, then whichever is negative is
// smaller.
return a_is_negative;
} else if (a.length() != b.length()) {
// If they have the same sign, then whichever has more
// characters is larger in magnitude. When the sign is negative,
// the longer (more digits) number is "more negative". When
// positive, the longer (more digits) number is "more positive".
return (a.length() < b.length()) != a_is_negative;
} else {
// Otherwise a lexicographic comparison of "a" and "b" will
// determine which string is larger in magnitude. Using the same
// logic above, we account for the "negative vs. positive"
// comparison.
return (a < b) != a_is_negative;
}
});
for (const auto &str : strings) {
std::cout << str << " ";
}
std::cout << std::endl;
}