Skip to content

Commit

Permalink
One more slight improvement to natural sorting to get strings staring…
Browse files Browse the repository at this point in the history
… with numbers in the right order to match windows natural sort.
  • Loading branch information
bluescan committed Aug 2, 2024
1 parent 7d5bfcd commit e823e17
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Modules/Foundation/Src/tStandard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ int tStd::tNstrcmp(const char* a, const char* b)
const char* origa = a;
const char* origb = b;

bool aStartsDig = a && tIsdigit(*a);
bool bStartsDig = b && tIsdigit(*b);

// This implementation of tNstrcmp is a modified version of the one written by GitHub user ClangPan.
while (*a && *b)
{
Expand Down Expand Up @@ -108,10 +111,10 @@ int tStd::tNstrcmp(const char* a, const char* b)
}

// If only the left char is a digit, we have a result.
if (aDigit) return +1;
if (aDigit) return aStartsDig ? -1 : +1;

// If only the right char is a digit, we have a result.
if (bDigit) return -1;
if (bDigit) return bStartsDig ? +1 : -1;

// compute the difference of both characters
int sign = tMath::tSign(tToLower(*a) - tToLower(*b));
Expand Down
4 changes: 4 additions & 0 deletions UnitTests/Src/TestFoundation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ tTestUnit(ListSort)
tList<MultiObj> multiObjList;

// Add items with an extension.
multiObjList.Append(new MultiObj("21Num.txt"));
multiObjList.Append(new MultiObj("7Num.txt"));
multiObjList.Append(new MultiObj("page100.txt"));
multiObjList.Append(new MultiObj("Page20.txt"));
multiObjList.Append(new MultiObj("Page4.txt"));
Expand All @@ -494,6 +496,8 @@ tTestUnit(ListSort)
multiObjList.Append(new MultiObj("page -8.txt"));

// Add the same items without an extension.
multiObjList.Append(new MultiObj("21Num"));
multiObjList.Append(new MultiObj("7Num"));
multiObjList.Append(new MultiObj("page100"));
multiObjList.Append(new MultiObj("Page20"));
multiObjList.Append(new MultiObj("Page4"));
Expand Down

0 comments on commit e823e17

Please sign in to comment.