How to implement a natural sort algorithm in c++?

后端 未结 8 1499
不思量自难忘°
不思量自难忘° 2020-11-30 10:21

I\'m sorting strings that are comprised of text and numbers. I want the sort to sort the number parts as numbers, not alphanumeric.

For example I want: abc1def, ...

相关标签:
8条回答
  • 2020-11-30 10:53

    To solve what is essentially a parsing problem a state machine (aka finite state automaton) is the way to go. Dissatisfied with the above solutions i wrote a simple one-pass early bail-out algorithm that beats C/C++ variants suggested above in terms of performance, does not suffer from numerical datatype overflow errors, and is easy to modify to add case insensitivity if required.

    sources can be found here

    0 讨论(0)
  • 2020-11-30 10:53
    // -1: s0 < s1; 0: s0 == s1; 1: s0 > s1
    static int numericCompare(const string &s0, const string &s1) {
        size_t i = 0, j = 0;
        for (; i < s0.size() && j < s1.size();) {
            string t0(1, s0[i++]);
            while (i < s0.size() && !(isdigit(t0[0]) ^ isdigit(s0[i]))) {
                t0.push_back(s0[i++]);
            }
            string t1(1, s1[j++]);
            while (j < s1.size() && !(isdigit(t1[0]) ^ isdigit(s1[j]))) {
                t1.push_back(s1[j++]);
            }
            if (isdigit(t0[0]) && isdigit(t1[0])) {
                size_t p0 = t0.find_first_not_of('0');
                size_t p1 = t1.find_first_not_of('0');
                t0 = p0 == string::npos ? "" : t0.substr(p0);
                t1 = p1 == string::npos ? "" : t1.substr(p1);
                if (t0.size() != t1.size()) {
                    return t0.size() < t1.size() ? -1 : 1;
                }
            }
            if (t0 != t1) {
                return t0 < t1 ? -1 : 1;
            }
        }
        return i == s0.size() && j == s1.size() ? 0 : i != s0.size() ? 1 : -1;
    }
    

    I am not very sure if it is you want, anyway, you can have a try:-)

    0 讨论(0)
提交回复
热议问题