Hello everyone. I drag a folder with numbered files into a playlist with sorting. Sorting occurs correctly if the files are numbered: 01, 02, …10, 11, etc.
But if the files are numbered: 1, 2, 3, … 11, 12, etc., then it turns out: 1, 10, 11…19, 2, 20, etc.
That is, sorting is done by the first character.
How can I sort files by two or three characters?
Here is the code:
I would get the filenames into a vector and sort it using the juce::String::getIntValue(). This function has the advantage, that it simply parses the string as long as the characters are numeric and ignores anything after that.
auto filesArray = documents.findChildFiles( juce::File::findFiles, false);
juce::Array<juce::String> names;
for (const auto& file : filesArray)
names.add (file.getFileName());
std::sort (names.begin(), names.end(), [](const auto& a, const auto& b)
{
return a.getIntValue() < b.getIntValue();
});
juce::Array<juce::File> sorted;
for (const auto& name : names)
sorted.add (documents.getChildFile (name);
Because std::Array has begin() and end() iterators, you can use the sort from the standard library.
It is a bit more complicated for a recursive search…
You might be able to compress the code, but this is the easiest version to read IMHO.