-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinesNaturalSort.cpp
More file actions
45 lines (36 loc) · 1.21 KB
/
SinesNaturalSort.cpp
File metadata and controls
45 lines (36 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "SinesNaturalSort.h"
namespace SinesNaturalSort {
std::vector<std::string> NaturalSorter::sort(std::vector<std::string>& links) {
std::sort(links.begin(), links.end(), compareLinks);
return links;
}
double NaturalSorter::extractChapterNumber(const std::string& url) {
static const std::regex chapter_pattern(R"(chapter[=\s]?(\d+(\.\d+)?))", std::regex::icase);
std::smatch match;
if (std::regex_search(url, match, chapter_pattern)) {
try {
return std::stod(match[1]);
} catch(...) {
return -1;
}
}
return -1;
}
bool NaturalSorter::compareLinks(const std::string& a, const std::string& b) {
double chapter_a = extractChapterNumber(a);
double chapter_b = extractChapterNumber(b);
// If both have valid chapter numbers, sort numerically
if (chapter_a != -1 && chapter_b != -1) {
return chapter_a < chapter_b;
}
// Prioritize links with chapter numbers
if (chapter_a != -1 && chapter_b == -1) {
return true;
}
if (chapter_a == -1 && chapter_b != -1) {
return false;
}
// If no chapter numbers found, maintain original order
return false;
}
} // namespace SinesNaturalSort