If both strings are numbers, compare them as such

This commit is contained in:
Andrzej Rybczak
2024-07-24 19:18:00 +02:00
parent af7550a69f
commit 6f8d12da8c
2 changed files with 16 additions and 14 deletions

View File

@@ -98,7 +98,7 @@ bool MoveToAlbum(NC::Menu<AlbumEntry> &albums, const std::string &primary_tag, c
struct SortSongs {
typedef NC::Menu<MPD::Song>::Item SongItem;
static const std::array<MPD::Song::GetFunction, 3> GetFuns;
static const std::array<MPD::Song::GetFunction, 4> GetFuns;
LocaleStringComparison m_cmp;
@@ -117,26 +117,16 @@ public:
return ret < 0;
}
// Sort by track numbers.
try {
ret = boost::lexical_cast<int>(a.getTags(&MPD::Song::getTrackNumber))
- boost::lexical_cast<int>(b.getTags(&MPD::Song::getTrackNumber));
} catch (boost::bad_lexical_cast &) {
ret = a.getTrackNumber().compare(b.getTrackNumber());
}
if (ret != 0)
return ret < 0;
// If track numbers are equal, sort by the display format.
return Format::stringify<char>(Config.song_library_format, &a)
< Format::stringify<char>(Config.song_library_format, &b);
}
};
const std::array<MPD::Song::GetFunction, 3> SortSongs::GetFuns = {{
const std::array<MPD::Song::GetFunction, 4> SortSongs::GetFuns = {{
&MPD::Song::getDate,
&MPD::Song::getAlbum,
&MPD::Song::getDisc
&MPD::Song::getDisc,
&MPD::Song::getTrackNumber,
}};
class SortAlbumEntries {

View File

@@ -34,10 +34,22 @@ bool hasTheWord(const std::string &s)
&& (s[3] == ' ');
}
long long strToLL(const char *s, size_t len)
{
long long n = 0;
while (len--)
n = 10*n + *s++ - '0';
return n;
}
}
int LocaleStringComparison::compare(const char *a, size_t a_len, const char *b, size_t b_len) const
{
// If both strings are numbers, compare them as such.
if (std::all_of(a, a + a_len, isdigit) && std::all_of(b, b + b_len, isdigit))
return strToLL(a, a_len) - strToLL(b, b_len);
size_t ac_off = 0, bc_off = 0;
if (m_ignore_the)
{