C++ string sort like a human being?

后端 未结 4 2227
灰色年华
灰色年华 2021-02-05 23:12

I would like to sort alphanumeric strings the way a human being would sort them. I.e., \"A2\" comes before \"A10\", and \"a\" certainly comes before \"Z\"! Is there any way to d

4条回答
  •  忘掉有多难
    2021-02-06 00:17

    Without any parsing, there's no way to compare human written numbers (high values first with leading zeroes stripped) and normal characters as part of the same string.

    The parsing doesn't need to be terribly complex though. A simple hash table to deal with things like case sensitivity and stripping special characters ('A'='a'=1,'B'='b'='2,... or 'A'=1,'a'=2,'B'=3,..., '-'=0(strip)), remap your string to an array of the hashed values, then truncate number cases (if a number is encountered and the last character was a number, multiply the last number by ten and add the current value to it).

    From there, sort as normal.

提交回复
热议问题