1
0
mirror of https://github.com/uowuo/abaddon.git synced 2025-03-04 03:03:16 -05:00

add util with AlphabeticalSort

This commit is contained in:
ouwou 2020-09-05 20:58:11 -04:00
parent f2369dff2c
commit 5e0a5bb964
4 changed files with 37 additions and 21 deletions

View File

@ -170,6 +170,7 @@
<ClInclude Include="discord\objects.hpp" />
<ClInclude Include="discord\websocket.hpp" />
<ClInclude Include="settings.hpp" />
<ClInclude Include="util.hpp" />
<ClInclude Include="windows\mainwindow.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -95,5 +95,8 @@
<ClInclude Include="dialogs\editmessage.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="util.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -1,6 +1,7 @@
#include "../abaddon.hpp"
#include "discord.hpp"
#include <cassert>
#include "../util.hpp"
DiscordClient::DiscordClient()
: m_http(DiscordAPI)
@ -84,27 +85,7 @@ std::vector<std::pair<Snowflake, GuildData>> 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;

31
util.hpp Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <functional>
#include <iterator>
template<typename T>
inline void AlphabeticalSort(typename T start, typename T end, std::function<std::string(const typename std::iterator_traits<T>::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];
});
}