Skip to content

Instantly share code, notes, and snippets.

@am-kantox
Created April 12, 2022 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save am-kantox/f28d34c139b58843b18b0d9ad17f542f to your computer and use it in GitHub Desktop.
Save am-kantox/f28d34c139b58843b18b0d9ad17f542f to your computer and use it in GitHub Desktop.
Naïve string sorter for Russian Cyrillic
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