Skip to content

Commit a3ae942

Browse files
committed
proper version sorting (#38) - not written with AI
1 parent d54b245 commit a3ae942

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

source/globals.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77
#elif _WIN32
88
#include <ShlObj_core.h>
99
#endif
10+
#include <cstdio>
11+
12+
ParsedVersion parseVersion(const std::string_view version) {
13+
int major, minor, patch, build;
14+
if (sscanf(version.data(), "%d.%d.%d%*c%d", &major, &minor, &patch, &build) == 4) {
15+
return {
16+
major,
17+
minor,
18+
patch,
19+
build
20+
};
21+
};
22+
// parse error, assume all 0s.
23+
return {};
24+
}
1025

1126
void launch_process(const std::string& command, int flags) {
1227
#if defined __APPLE__ || defined __linux__
@@ -94,3 +109,4 @@ void dpi_scale(wxWindow* window){
94109
//set the minimum size
95110
window->SetSizeHints(wxSize(minw, minh));
96111
}
112+

source/globals.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,9 @@ struct project {
139139
return this->path == other.path;
140140
}
141141
};
142+
143+
struct ParsedVersion {
144+
int major = 0, minor = 0, patch = 0, build = 0;
145+
};
146+
147+
ParsedVersion parseVersion(const std::string_view version);

source/interface_derived.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,24 @@ int wxCALLBACK MainFrameDerived::CompareItems(wxIntPtr item1, wxIntPtr item2, wx
741741
case 0: // Name
742742
result = p1.name.compare(p2.name);
743743
break;
744-
case 1: // Version
745-
result = p1.version.compare(p2.version);
744+
case 1: { // Version
745+
auto v1 = parseVersion(p1.version);
746+
auto v2 = parseVersion(p2.version);
747+
748+
auto v1t = std::tie(v1.major, v1.minor, v1.patch, v1.build);
749+
auto v2t = std::tie(v2.major, v2.minor, v2.patch, v2.build);
750+
751+
if (v1t < v2t) {
752+
result = -1;
753+
}
754+
else if (v1t > v2t) {
755+
result = 1;
756+
}
757+
else {
758+
result = 0;
759+
}
746760
break;
761+
}
747762
case 2: // Last Modified
748763
// Compare dates - note: string comparison may not work correctly for all date formats
749764
// For dates, default to newest first (reverse the comparison)

0 commit comments

Comments
 (0)