openframeworks / openframeworks/openFrameworks
ofDirectory Version Ordering
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 10.4k
- Forks
- 2.6k
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 9
Description
I propose an improvement on ofDirectory::sort() regarding ofDirectory natural sort order. The following code is a bit longer, but it can deal with prefix + ofToInt(n) + extension style files like ls -v.
ofFileUtils.cpp:
bool natural(const ofFile& a, const ofFile& b) {
string aname = a.getBaseName();
string bname = b.getBaseName();
string aPrefix;
string bPrefix;
int aPrefixLength = 0;
int bPrefixLength = 0;
for(int i = 0; i < aname.length(); i++){
if(isdigit(aname.at(i))){
break;
}
aPrefixLength++;
}
for(int i = 0; i < bname.length(); i++){
if(isdigit(bname.at(i))){
break;
}
bPrefixLength++;
}
aPrefix = aname.substr(0, aPrefixLength);
bPrefix = bname.substr(0, bPrefixLength);
// if prefixes are different
if(aPrefix.compare(bPrefix) != 0) {
return aPrefix.compare(bPrefix) < 0;
}
// if prefixes are the same, result is ascending version
int aVersionLength = 0;
int bVersionLength = 0;
for(int i = aPrefixLength; i < aname.length(); i++){
if(!isdigit(aname.at(i))){
break;
}
aVersionLength++;
}
for(int i = bPrefixLength; i < bname.length(); i++){
if(!isdigit(bname.at(i))){
break;
}
bVersionLength++;
}
int aVersion = ofToInt(aname.substr(aPrefixLength, aVersionLength));
int bVersion = ofToInt(bname.substr(bPrefixLength, bVersionLength));
if(aVersion != bVersion){
return aVersion < bVersion;
}else{
return a.getFileName().compare(b.getFileName()) < 0;
}
}
Original output:
[notice ] file 1 = DSC09302.jpg
[notice ] file 2 = DSC09305.jpg
[notice ] file 3 = DSC09307.jpg
[notice ] file 4 = DSC09310.jpg
[notice ] file 5 = DSC09311.jpg
[notice ] file 6 = DSC09315.jpeg
[notice ] file 7 = DSC09315.jpg
[notice ] file 8 = DSC09315abc.jpg
[notice ] file 9 = DSC09316.jpg
[notice ] file 10 = DSC09318.jpg
[notice ] file 11 = DSC09320.jpg
[notice ] file 12 = DSC09322.jpg
[notice ] file 13 = DSC09324.jpg
[notice ] file 14 = DSC09326.jpg
[notice ] file 15 = DSC09327.jpg
[notice ] file 16 = DSC09329.jpg
[notice ] file 17 = DSC09330.jpg
[notice ] file 18 = DSC09334.jpg
[notice ] file 19 = DSC09336.jpg
[notice ] file 20 = DSC09337.jpg
[notice ] file 21 = DSC09341.jpg
[notice ] file 22 = DSC09343.jpg
[notice ] file 23 = DSC09344.jpg
[notice ] file 24 = DSC09347.jpg
[notice ] file 25 = DSC11.jpg
[notice ] file 26 = DSC900.jpg
[notice ] file 27 = a200.jpg
After change:
[notice ] file 1 = DSC11.jpg
[notice ] file 2 = DSC900.jpg
[notice ] file 3 = DSC09302.jpg
[notice ] file 4 = DSC09305.jpg
[notice ] file 5 = DSC09307.jpg
[notice ] file 6 = DSC09310.jpg
[notice ] file 7 = DSC09311.jpg
[notice ] file 8 = DSC09315.jpeg
[notice ] file 9 = DSC09315.jpg
[notice ] file 10 = DSC09315abc.jpg
[notice ] file 11 = DSC09316.jpg
[notice ] file 12 = DSC09318.jpg
[notice ] file 13 = DSC09320.jpg
[notice ] file 14 = DSC09322.jpg
[notice ] file 15 = DSC09324.jpg
[notice ] file 16 = DSC09326.jpg
[notice ] file 17 = DSC09327.jpg
[notice ] file 18 = DSC09329.jpg
[notice ] file 19 = DSC09330.jpg
[notice ] file 20 = DSC09334.jpg
[notice ] file 21 = DSC09336.jpg
[notice ] file 22 = DSC09337.jpg
[notice ] file 23 = DSC09341.jpg
[notice ] file 24 = DSC09343.jpg
[notice ] file 25 = DSC09344.jpg
[notice ] file 26 = DSC09347.jpg
[notice ] file 27 = a200.jpg
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in ofFileUtils.cpp at ofDirectory::sort() and review the proposed natural() comparator. Check how filenames such as DSC11.jpg, DSC900.jpg, and DSC09302.jpg should be ordered, including equal numeric prefixes and differing extensions. Done means sorting produces the natural-order output shown in the issue without changing unrelated ordering.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100