# `MapSorter.SortSpecs`
[🔗](https://github.com/RaymondLoranger/map_sorter/blob/main/lib/map_sorter/sort_specs.ex#L1)

Converts the AST of sort specs into the AST of an `Enum.sort/2` compare
function.

# `t`

```elixir
@type t() :: [MapSorter.SortSpec.t()]
```

A list of sort specs

# `to_quoted`

```elixir
@spec to_quoted(t() | Macro.t()) :: {:ok, Macro.t()} | {:error, Macro.t()}
```

Converts `sort_specs` into the AST of an `Enum.sort/2` compare function.

## Examples

    # To run multiple doctests sequentially, remove empty lines between them.
    iex> alias MapSorter.{SortSpecs, Log}
    iex> Log.debug(:doctest_id, {:sort_specs_id_1, __ENV__})
    iex> sort_specs = [:dob]
    iex> {:ok, comp_fun_ast} = SortSpecs.to_quoted(sort_specs)
    iex> {:&, _meta, [args]} = comp_fun_ast
    iex> {comp_fun, []} = Code.eval_quoted(comp_fun_ast)
    iex> is_function(comp_fun, 2) and Macro.to_string(args)
    """
    cond do
      &1[:dob] < &2[:dob] -> true
      &1[:dob] > &2[:dob] -> false
      true -> true or &1 > &2
    end\
    """
    iex> alias MapSorter.{SortSpecs, Log}
    iex> Log.debug(:doctest_id, {:sort_specs_id_2, __ENV__})
    iex> sort_specs = fn -> [desc: :dob, asc: :bmi] end
    iex> {:ok, comp_fun_ast} = sort_specs.() |> SortSpecs.to_quoted()
    iex> {:&, _meta, [args]} = comp_fun_ast
    iex> {comp_fun, []} = Code.eval_quoted(comp_fun_ast)
    iex> is_function(comp_fun, 2) and Macro.to_string(args)
    """
    cond do
      &1[:dob] > &2[:dob] -> true
      &1[:dob] < &2[:dob] -> false
      &1[:bmi] < &2[:bmi] -> true
      &1[:bmi] > &2[:bmi] -> false
      true -> true or &1 > &2
    end\
    """
    iex> alias MapSorter.{SortSpecs, Log}
    iex> Log.debug(:doctest_id, {:sort_specs_id_3, __ENV__})
    iex> {:ok, comp_fun_ast} = SortSpecs.to_quoted([:dob, desc: :likes])
    iex> {:&, _meta, [args]} = comp_fun_ast
    iex> {comp_fun, []} = Code.eval_quoted(comp_fun_ast)
    iex> is_function(comp_fun, 2) and Macro.to_string(args)
    """
    cond do
      &1[:dob] < &2[:dob] -> true
      &1[:dob] > &2[:dob] -> false
      &1[:likes] > &2[:likes] -> true
      &1[:likes] < &2[:likes] -> false
      true -> true or &1 > &2
    end\
    """
    iex> alias MapSorter.{SortSpecs, Log}
    iex> Log.debug(:doctest_id, {:sort_specs_id_4, __ENV__})
    iex> sort_specs = quote do: Tuple.to_list({:dob, {:desc, :likes}})
    iex> {:ok, comp_fun_ast} = SortSpecs.to_quoted(sort_specs)
    iex> {{:., _, nested_args}, _meta, [args]} = comp_fun_ast
    iex> {comp_fun, []} = Code.eval_quoted(comp_fun_ast)
    iex> is_function(comp_fun, 2) and
    ...> {Macro.to_string(nested_args), Macro.to_string(args)}
    {"[MapSorter.Compare, :fun]", "Tuple.to_list({:dob, {:desc, :likes}})"}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
