Skip to content

Commit ccfc8f8

Browse files
committed
fix: sar_startdemosfolder sorting
1 parent 7b85b68 commit ccfc8f8

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/Modules/EngineDemoPlayer.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,36 @@ CON_COMMAND_F_COMPLETION(sar_startdemosfolder, "sar_startdemosfolder <folder nam
593593
}
594594
}
595595

596-
std::sort(engine->demoplayer->demoQueue.begin(), engine->demoplayer->demoQueue.end());
596+
std::sort(engine->demoplayer->demoQueue.begin(), engine->demoplayer->demoQueue.end(),
597+
[](const std::string &a, const std::string &b) {
598+
auto extractNumber = [](const std::string &str) -> int {
599+
size_t underscorePos = str.find_last_of('_');
600+
if (underscorePos != std::string::npos) {
601+
try {
602+
return std::stoi(str.substr(underscorePos + 1));
603+
} catch (std::invalid_argument &) {
604+
return 0;
605+
}
606+
}
607+
return 0;
608+
};
609+
610+
std::string aName = a, bName = b;
611+
aName.erase(aName.size() - 4);
612+
bName.erase(bName.size() - 4);
613+
size_t aSlash = aName.find_last_of('/'), bSlash = bName.find_last_of('/');
614+
if (aSlash != std::string::npos) aName = aName.substr(aSlash + 1);
615+
if (bSlash != std::string::npos) bName = bName.substr(bSlash + 1);
616+
size_t aUnder = aName.find_last_of('_'), bUnder = bName.find_last_of('_');
617+
std::string aBase = (aUnder != std::string::npos) ? aName.substr(0, aUnder) : aName;
618+
std::string bBase = (bUnder != std::string::npos) ? bName.substr(0, bUnder) : bName;
619+
if (aBase == bBase) {
620+
int aNum = extractNumber(aName), bNum = extractNumber(bName);
621+
if (aNum != 0 && bNum != 0) return aNum < bNum;
622+
return aNum == 0;
623+
}
624+
return aBase < bBase;
625+
});
597626

598627
engine->demoplayer->demoQueueSize = engine->demoplayer->demoQueue.size();
599628
engine->demoplayer->currentDemoID = 0;

0 commit comments

Comments
 (0)