Created
April 12, 2022 03:50
-
-
Save am-kantox/f28d34c139b58843b18b0d9ad17f542f to your computer and use it in GitHub Desktop.
Naïve string sorter for Russian Cyrillic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule RuSorter do | |
def compare(<<"ё", _::binary>>, <<"ё", _::binary>>), do: :eq | |
def compare(<<"Ё", _::binary>>, <<"Ё", _::binary>>), do: :eq | |
def compare(<<"ё", _::binary>>, <<c::utf8, _::binary>>) when c in 1072..1077, do: :gt | |
def compare(<<c::utf8, _::binary>>, <<"ё", _::binary>>) when c in 1072..1077, do: :lt | |
def compare(<<"ё", _::binary>>, <<c::utf8, _::binary>>) when c in 1078..1103, do: :lt | |
def compare(<<c::utf8, _::binary>>, <<"ё", _::binary>>) when c in 1078..1103, do: :gt | |
def compare(<<"Ё", _::binary>>, <<c::utf8, _::binary>>) when c in 1040..1045, do: :gt | |
def compare(<<c::utf8, _::binary>>, <<"Ё", _::binary>>) when c in 1040..1045, do: :lt | |
def compare(<<"Ё", _::binary>>, <<c::utf8, _::binary>>) when c in 1046..1071, do: :lt | |
def compare(<<c::utf8, _::binary>>, <<"Ё", _::binary>>) when c in 1046..1071, do: :gt | |
def compare(<<c1::utf8, _::binary>>, <<c2::utf8, _::binary>>) when c1 < c2, do: :lt | |
def compare(_, _), do: :gt | |
end | |
##################################### | |
### TESTS ### | |
##################################### | |
iex|💧|1 ▸ ["ель", "ёж", "яблоко", "железо"] |> Enum.sort(RuSorter) | |
#⇒ ["ель", "ёж", "железо", "яблоко"] | |
iex|💧|2 ▸ ["ель", "ёж", "яблоко", "железо"] |> Enum.map(&String.upcase/1) |> Enum.sort(RuSorter) | |
#⇒ ["ЕЛЬ", "ЁЖ", "ЖЕЛЕЗО", "ЯБЛОКО"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment