How to Sort Strings in C++

Hi. So I have a little problem. I’m trying to convert some code from a Python project of mine into C++. In it, I have an array that has strings in it such as “07-09-2023.log”, “07-11-2023.log”, “07-11-2023_1.log”, “07-11-2023_12.log”, and so forth. I need to sort these so that the dates will be in order, and duplicate dates will be in order of the number after the underscore. In Python, I used this function as the sorting key:

def logFileSort(inp):
    if '_' in inp:
        return int(inp[inp.index("_") + 1:-4])
        return 0

This would return the number after the underscore if present, and if not, zero.

In C++, custom sorting algorithms work differently. The function will be provided two arguments. The comparator function should return true if the first element should come before the second element, and false otherwise.

How do I do what I want to do using the C++ system?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.