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

后端 未结 8 1531
不思量自难忘°
不思量自难忘° 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:27

    Several natural sort implementations for C++ are available. A brief review:

    • natural_sort<> - based on Boost.Regex.
      • In my tests, it's roughly 20 times slower than other options.
    • Dirk Jagdmann's alnum.hpp, based on Dave Koelle's alphanum algorithm
      • Potential integer overlow issues for values over MAXINT
    • Martin Pool's natsort - written in C, but trivially usable from C++.
      • The only C/C++ implementation I've seen to offer a case insensitive version, which would seem to be a high priority for a "natural" sort.
      • Like the other implementations, it doesn't actually parse decimal points, but it does special case leading zeroes (anything with a leading 0 is assumed to be a fraction), which is a little weird but potentially useful.
      • PHP uses this algorithm.

提交回复
热议问题