From 5e0a5bb964a54c396186c36a11ec56c788f03082 Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Sat, 5 Sep 2020 20:58:11 -0400 Subject: add util with AlphabeticalSort --- Abaddon.vcxproj | 1 + Abaddon.vcxproj.filters | 3 +++ discord/discord.cpp | 23 ++--------------------- util.hpp | 31 +++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 21 deletions(-) create mode 100644 util.hpp diff --git a/Abaddon.vcxproj b/Abaddon.vcxproj index 7ac7422..ca01a0f 100644 --- a/Abaddon.vcxproj +++ b/Abaddon.vcxproj @@ -170,6 +170,7 @@ + diff --git a/Abaddon.vcxproj.filters b/Abaddon.vcxproj.filters index 27b1d17..c361054 100644 --- a/Abaddon.vcxproj.filters +++ b/Abaddon.vcxproj.filters @@ -95,5 +95,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/discord/discord.cpp b/discord/discord.cpp index 4417cf5..9cf9df4 100644 --- a/discord/discord.cpp +++ b/discord/discord.cpp @@ -1,6 +1,7 @@ #include "../abaddon.hpp" #include "discord.hpp" #include +#include "../util.hpp" DiscordClient::DiscordClient() : m_http(DiscordAPI) @@ -84,27 +85,7 @@ std::vector> DiscordClient::GetUserSortedGuilds( } else { // default sort is alphabetic for (auto &it : m_guilds) sorted_guilds.push_back(it); - std::sort(sorted_guilds.begin(), sorted_guilds.end(), [&](auto &a, auto &b) -> bool { - std::string &s1 = a.second.Name; - std::string &s2 = b.second.Name; - - if (s1.empty() || s2.empty()) - return s1 < s2; - - bool ac[] = { - !isalnum(s1[0]), - !isalnum(s2[0]), - isdigit(s1[0]), - isdigit(s2[0]), - isalpha(s1[0]), - isalpha(s2[0]), - }; - - if ((ac[0] && ac[1]) || (ac[2] && ac[3]) || (ac[4] && ac[5])) - return s1 < s2; - - return ac[0] || ac[5]; - }); + AlphabeticalSort(sorted_guilds.begin(), sorted_guilds.end(), [](auto &pair) { return pair.second.Name; }); } return sorted_guilds; diff --git a/util.hpp b/util.hpp new file mode 100644 index 0000000..48cd73c --- /dev/null +++ b/util.hpp @@ -0,0 +1,31 @@ +#pragma once +#include +#include +#include +#include +#include + +template +inline void AlphabeticalSort(typename T start, typename T end, std::function::value_type &)> get_string) { + std::sort(start, end, [&](const auto &a, const auto &b) -> bool { + const std::string &s1 = get_string(a); + const std::string &s2 = get_string(b); + + if (s1.empty() || s2.empty()) + return s1 < s2; + + bool ac[] = { + !isalnum(s1[0]), + !isalnum(s2[0]), + !!isdigit(s1[0]), + !!isdigit(s2[0]), + !!isalpha(s1[0]), + !!isalpha(s2[0]), + }; + + if ((ac[0] && ac[1]) || (ac[2] && ac[3]) || (ac[4] && ac[5])) + return s1 < s2; + + return ac[0] || ac[5]; + }); +} -- cgit v1.2.3