Skip to content

faster sort and option for natural, fast and by date #7509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions libs/openFrameworks/utils/ofFileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,24 @@ static bool natural(const ofFile& a, const ofFile& b) {
}
}


//------------------------------------------------------------------------------------------------------------
struct StringSort{
of::filesystem::path path;
string basename;
int nameInt;
string stringInt;
};

//------------------------------------------------------------------------------------------------------------
static bool naturalStr(const StringSort& a, const StringSort& b) {
if(a.stringInt == a.basename && b.stringInt == b.basename) {
return a.nameInt < b.nameInt;
} else {
return a.path < b.path;
}
}

//------------------------------------------------------------------------------------------------------------
static bool byDate(const ofFile& a, const ofFile& b) {
auto ta = of::filesystem::last_write_time(a);
Expand All @@ -1502,11 +1520,49 @@ void ofDirectory::sortByDate() {
}

//------------------------------------------------------------------------------------------------------------
void ofDirectory::sort(){
void ofDirectory::sort(const SortMode & mode){
if(files.empty() && !myDir.empty()){
listDir();
}
ofSort(files, natural);

if( mode == ofDirectory::SORT_NATURAL ){
vector <StringSort> sort;
sort.reserve(files.size());

for( auto & f : files ){
StringSort ss;
ss.path = f.path();
ss.basename = f.getBaseName();
ss.nameInt = ofToInt(ss.basename);
ss.stringInt = ofToString(ss.nameInt);
sort.push_back(ss);
}

ofSort(sort, naturalStr);
files.clear();
files.reserve(sort.size());
for( auto & s : sort ){
files.emplace_back( s.path , ofFile::Reference);
}
}
else if(mode == ofDirectory::SORT_FAST){
std::vector <string> sort;
sort.reserve(files.size());

for( auto & f : files ){
string ss = f.getFileName();
sort.push_back(ss);
}

std::sort(sort.begin(), sort.end());
files.clear();
files.reserve(sort.size());
for( auto & s : sort ){
files.emplace_back( myDir / of::filesystem::path(s), ofFile::Reference);
}
}else if(mode == ofDirectory::SORT_BY_DATE){
sortByDate();
}
}

//------------------------------------------------------------------------------------------------------------
Expand Down
9 changes: 8 additions & 1 deletion libs/openFrameworks/utils/ofFileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1101,11 +1101,18 @@ class ofDirectory{
/// This is for backwards compatibility with ofxDirList.
void reset();

typedef enum{
SORT_FAST,
SORT_NATURAL,
SORT_BY_DATE
}SortMode;

/// Sort the directory contents list alphabetically.
///
/// \warning Call listDir() before using this function or there will be
/// nothing to sort.
void sort();
/// \param SortMode options are SORT_FAST, SORT_NATURAL (default) or SORT_BY_DATE
void sort(const SortMode & mode = SORT_NATURAL);

/// Sort the directory contents list by date.
///
Expand Down