Sort files ascending of data folder

Hi! i’m trying to sort files(img) in ascending order by the first number of the file. That are in the data folder the files are name like this 300-zl.jpg i’m using splitstring to separate the first number, i’m able to sort the numbers but how do i sort the files been thinking maybe to compare the numbers with the files and re-saved? any advice will be helpfull.

Have you looked at ofDirectory? It’ll give you all the files in a folder which you can filter by file type and then sort.

There’s an example in the examples/utils called dirListExample that might also help.

Good luck!

Yeah, but can only sort by files by name.

something like:

std::sort(files.begin(), files.end(), [](std::string & file1, std::string & file2){
     auto split1 = ofSplitString(file1, "-");
     auto split2 = ofSplitString(file2, "-");
     return ofToInt(split[0]) < ofToInt(split2[0]);

should sort a vector of strings with the format you posted. (i haven’t tested it so there might be something wrong but i guess you get the idea

also with ofDirectory you could do:

std::sort(dir.begin(), dir.end(), [](ofFile & file1, ofFile & file2){
     auto split1 = ofSplitString(file1.getFileName(), "-");
     auto split2 = ofSplitString(file2.getFileName(), "-");
     return ofToInt(split[0]) < ofToInt(split2[0]);

ah sorry I misunderstood the question.

Yeah, i get the idea i’m just having some syntax errors not sure hove to write straight c++ also it should have a loop somewhere not releate to “auto” variable.

no, you don’t need any loops, that call already goes through the whole vector and sorts it using the comparison you make in the passed function.

i think the original is missing a comma, i’ve fixed it there but please post any errors you are having so we can see what’s going on

Hi Arturo, code is running now i get a bunch of errors like:

No matching function for call to object of type ('lamdba at …/…/…

No matching function for call to '_sort3

No matching function for call to '_sort4

i’ve just tried and the second option (with ofDirectory) won’t work and there’s really no way to make it work directly at the moment. but the first one with files being a vector of stings works without problem for me.

if you keep having errors, you should post a small example showing the problem and the exact error you are having otherwise it’s really difficult to figure out what might be the problem

Okay, just try the first and i’m getting the same errors.
I made an
vector files;
maybe should i write something in the setup?