diff --git a/src/libs/subsonic/impl/SubsonicResource.cpp b/src/libs/subsonic/impl/SubsonicResource.cpp index df5228e5..0552311f 100644 --- a/src/libs/subsonic/impl/SubsonicResource.cpp +++ b/src/libs/subsonic/impl/SubsonicResource.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -1071,46 +1072,30 @@ handleGetArtistsRequestCommon(RequestContext& context, bool id3) break; } - Response::Node* currentIndexNode {}; - char currentIndex{}; - - Response::Node* unknownIndexNode {}; - auto getOrCreateUnknownIndexNode {[&] - { - if (!unknownIndexNode) - { - unknownIndexNode = &artistsNode.createArrayChild("index"); - unknownIndexNode->setAttribute("name", "?"); - } - - return unknownIndexNode; - }}; - - auto getOrCreateIndexNode {[&](char first) - { - if (!currentIndexNode || currentIndex != first) - { - currentIndexNode = &artistsNode.createArrayChild("index"); - currentIndexNode->setAttribute("name", std::string {first}); - currentIndex = first; - } - - return currentIndexNode; - }}; - + std::map> artistsSortedByFirstChar; const RangeResults artists {Artist::find(context.dbSession, parameters)}; for (const ArtistId artistId : artists.results) { const Artist::pointer artist {Artist::find(context.dbSession, artistId)}; const std::string& sortName {artist->getSortName()}; - Response::Node* indexNode{}; + char sortChar; if (sortName.empty() || !std::isalpha(sortName[0])) - indexNode = getOrCreateUnknownIndexNode(); + sortChar = '?'; else - indexNode = getOrCreateIndexNode(std::toupper(sortName[0])); + sortChar = std::toupper(sortName[0]); - indexNode->addArrayChild("artist", artistToResponseNode(user, artist, id3)); + artistsSortedByFirstChar[sortChar].push_back(artist); + } + + + for (const auto& [sortChar, artists] : artistsSortedByFirstChar) + { + Response::Node& indexNode {artistsNode.createArrayChild("index")}; + indexNode.setAttribute("name", std::string {sortChar}); + + for (const Artist::pointer& artist :artists) + indexNode.addArrayChild("artist", artistToResponseNode(user, artist, id3)); } return response;