Skip to content

Latest commit

 

History

History
34 lines (27 loc) · 1.34 KB

README.md

File metadata and controls

34 lines (27 loc) · 1.34 KB

MultiSort

Build Status Version Hex Docs Download License Last Updated

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.