# Ecto order\_by

**URL:** <https://forum.elixirforum.com/t/ecto-order-by/41891>\
**Category:** Questions\
**Tags:** ecto, learning-elixir\
**Created:** [August 24, 2021, 9:15pm UTC](https://forum.elixirforum.com/t/ecto-order-by/41891 "2021-08-24T21:15:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grangerelixx](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@grangerelixx](https://forum.elixirforum.com/u/grangerelixx)\
**Post date:** [August 24, 2021, 9:15pm UTC](https://forum.elixirforum.com/t/ecto-order-by/41891/1 "2021-08-24T21:15:56Z")

</div>

In a case where there is default `order_by :name :asc`, if `order_by :type :desc` is given, does ecto automatically order them by `type` first and then `name`?

```elixir
%Post{
name : A,
type: free
}

%Post{
name : B,
type: paid
}

%Post{
name : C,
type: free
}

%Post{
name : D,
type: paid
}

```

In this case, it returns in this order `Post B, Post D, Post A, Post C`. This is my expected result but I just want to know how this happens.

Thanks! 😃

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [August 24, 2021, 9:22pm UTC](https://forum.elixirforum.com/t/ecto-order-by/41891/2 "2021-08-24T21:22:48Z")

</div>

Show us how you construct the Ecto query?

---

<div class="post-metadata">

**Author:** ![grangerelixx](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@grangerelixx](https://forum.elixirforum.com/u/grangerelixx)\
**Post date:** [August 24, 2021, 9:32pm UTC](https://forum.elixirforum.com/t/ecto-order-by/41891/3 "2021-08-24T21:32:52Z")

</div>

```elixir
def list_posts(criteria \\ %{}) do
    {sort_by, sort_order} = sorters(criteria) || {:name, :asc}

    from(p in Post)
    |> order_by({^sort_order, ^sort_by})
    |>Repo.paginate(
      query,
      [
        before: Map.get(criteria, :before_cursor),
        after: Map.get(criteria, :after_cursor),
        cursor_fields: [{sort_by, sort_order}, :id],
        limit: Map.get(criteria, :limit, 10)
      ])
end

defp sorters(%{sort: %{sort_by: sort_by, sort_order: sort_order}}) do
    {sort_by, sort_order}
  end

```

I call the function like this `Posts.list_posts(%{sort: %{sort_by: :type, sort_order: :desc}, limit: 2})`

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [August 24, 2021, 9:58pm UTC](https://forum.elixirforum.com/t/ecto-order-by/41891/4 "2021-08-24T21:58:48Z")

</div>

In this case, yes. However sorting by tuple is non-standard behaviour and instead you should provide list of fields instead, so it will be more SQL standard conformant.

---

<div class="post-metadata">

**Author:** ![grangerelixx](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@grangerelixx](https://forum.elixirforum.com/u/grangerelixx)\
**Post date:** [August 24, 2021, 10:04pm UTC](https://forum.elixirforum.com/t/ecto-order-by/41891/5 "2021-08-24T22:04:29Z")

</div>

Do you mind giving more clarity on how it is able to sort by both fields(type and name) when only type is given as sort\_by?
