Skip to content

Commit

Permalink
Merge pull request #432 from woylie/sort-descending-first
Browse files Browse the repository at this point in the history
allow using descending order as initial order
  • Loading branch information
woylie authored Nov 18, 2023
2 parents 46bf7b1 + 235857c commit a6856b3
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions lib/flop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,20 @@ defmodule Flop do
iex> flop.order_directions
[:desc_nulls_last]
This also allows you to sort in descending order initially.
iex> directions = {:desc, :asc}
iex> flop = push_order(%Flop{}, :ttfb, directions: directions)
iex> flop.order_by
[:ttfb]
iex> flop.order_directions
[:desc]
iex> flop = push_order(flop, :ttfb, directions: directions)
iex> flop.order_by
[:ttfb]
iex> flop.order_directions
[:asc]
If a string is passed as the second argument, it will be converted to an atom
using `String.to_existing_atom/1`. If the atom does not exist, the `Flop`
struct will be returned unchanged.
Expand Down Expand Up @@ -2061,11 +2075,19 @@ defmodule Flop do
do: reverse_direction(current_direction)

defp new_order_direction(0, current_direction, {_asc, desc})
when is_asc_direction(current_direction) and is_direction(desc),
when is_asc_direction(current_direction) and is_desc_direction(desc),
do: desc

defp new_order_direction(0, current_direction, {desc, _asc})
when is_asc_direction(current_direction) and is_desc_direction(desc),
do: desc

defp new_order_direction(0, current_direction, {asc, _desc})
when is_desc_direction(current_direction) and is_direction(asc),
when is_desc_direction(current_direction) and is_asc_direction(asc),
do: asc

defp new_order_direction(0, current_direction, {_desc, asc})
when is_desc_direction(current_direction) and is_asc_direction(asc),
do: asc

defp new_order_direction(0, _current_direction, directions) do
Expand Down

0 comments on commit a6856b3

Please sign in to comment.