# `Bylaw.Credo.Check.Ecto.PreferOrderByOverRepoAllEnumSort`
[🔗](https://github.com/ryanzidago/bylaw/blob/v0.4.0/lib/bylaw/credo/check/ecto/prefer_order_by_over_repo_all_enum_sort.ex#L1)

## Basics

> #### This check is disabled by default. {: .neutral}
>
> [Learn how to enable it](`e:credo:config_file.html#checks`) via `.credo.exs`.

This check has a base priority of `high` and works with any version of Elixir.

## Explanation

Prefer ordering Ecto queries in the database over sorting rows in memory.

Database-side ordering avoids materializing all matching rows before sorting
them in Elixir, reducing application memory and work. It also lets the
database combine ordering with filtering, limits, and indexes before rows
cross the database boundary.

## Examples

Avoid:

    query
    |> Repo.all()
    |> Enum.sort()

    query
    |> Repo.all()
    |> Enum.sort_by(& &1.inserted_at)

Prefer adding `order_by` to the query before calling `Repo.all`.

This check reports only direct `Repo.all` followed by `Enum.sort` or
`Enum.sort_by`. Sorting after an intermediate transformation may have
different semantics and is outside this check's scope.

This check uses static AST analysis and cannot infer the equivalent
`order_by` expression, so it does not provide an automatic rewrite.

## Check-Specific Parameters

*There are no specific parameters for this check.*

## General Parameters

Like with all checks, [general params](`e:credo:check_params.html`) can be applied.

Parameters can be configured via the [`.credo.exs` config file](`e:credo:config_file.html`).

---

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