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 --- util.hpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 util.hpp (limited to 'util.hpp') 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