How to sort file names with numbers and alphabets in order in C?

后端 未结 6 1558
青春惊慌失措
青春惊慌失措 2020-12-09 11:47

I have used the following code to sort files in alphabetical order and it sorts the files as shown in the figure:

for(int i = 0;i < maxcnt;i++) 
{
    fo         


        
6条回答
  •  清歌不尽
    2020-12-09 12:06

    Taking into account that this has a c++ tag, you could elaborate on @Joseph Quinsey's answer and create a natural_less function to be passed to the standard library.

    using namespace std;
    
    bool natural_less(const string& lhs, const string& rhs)
    {
        return strcasecmp_withNumbers(lhs.c_str(), rhs.c_str()) < 0;
    }
    
    void example(vector& data)
    {
        std::sort(data.begin(), data.end(), natural_less);
    }
    

    I took the time to write some working code as an exercise https://github.com/kennethlaskoski/natural_less

提交回复
热议问题