diff --git a/src/screens/media_library.cpp b/src/screens/media_library.cpp index df2b5866..1b046117 100644 --- a/src/screens/media_library.cpp +++ b/src/screens/media_library.cpp @@ -98,7 +98,7 @@ bool MoveToAlbum(NC::Menu &albums, const std::string &primary_tag, c struct SortSongs { typedef NC::Menu::Item SongItem; - static const std::array GetFuns; + static const std::array GetFuns; LocaleStringComparison m_cmp; @@ -117,26 +117,16 @@ public: return ret < 0; } - // Sort by track numbers. - try { - ret = boost::lexical_cast(a.getTags(&MPD::Song::getTrackNumber)) - - boost::lexical_cast(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(Config.song_library_format, &a) < Format::stringify(Config.song_library_format, &b); } }; -const std::array SortSongs::GetFuns = {{ +const std::array SortSongs::GetFuns = {{ &MPD::Song::getDate, &MPD::Song::getAlbum, - &MPD::Song::getDisc + &MPD::Song::getDisc, + &MPD::Song::getTrackNumber, }}; class SortAlbumEntries { diff --git a/src/utility/comparators.cpp b/src/utility/comparators.cpp index a9c0d339..49e3b97d 100644 --- a/src/utility/comparators.cpp +++ b/src/utility/comparators.cpp @@ -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) {