Natural Sorting algorithms

Just for reference, here is the same approach with a custom comparator. I didn’t bench these but since it doesn’t build tuples it is probably faster:

defmodule Natural do
  def natural_sort(enumerable) do
    Enum.sort(enumerable, __MODULE__)
  end

  def compare(x, y) do
    case do_compare(String.upcase(x), String.upcase(y)) do
      :eq -> do_compare(x, y)
      lt_or_gt -> lt_or_gt
    end
  end

  defp do_compare(x, x), do: :eq
  defp do_compare(x, y) when x > y, do: :gt
  defp do_compare(_, _), do: :lt
end