Easily sort by multiple fields in a readable manner. Inspired by Ecto/SQL order by.
Sort posts first by title and then by descending date:
posts
|> MultiSort.by([
{:asc, &1.title}
# Pass Date module as third element because we need to use Date.compare/2 to compare dates
{:desc, &1.date, Date},
])
Sort posts first by category according to order list and then by title:
post_category_order = [:business, :sports, :politics]
posts
|> MultiSort.by([
{:asc, &1.category, post_category_order},
{:asc, &1.title}
])
See docs for more information and examples.