Reputation: 1
I have multiple criteria for sorting
I have a sort order that I specify. (I spit out 1, 2, 3, 4 base on the criteria I have)
I have step 1 and 2 working. Stuck on step 3. The operation is also expensive, so I don't want to iterate the list more than once if I can avoid it.
I understand I could have first filter the list into 2 different arrays (such as group sort_order 1 to 3 into one array) and sort_order 4 into another array and then call reverse on the second array. But that would require me to iterate the list multiple times.
@assignments.sort_by do |a|
sort_date = if(a.sort_order == 1 || a.sort_order == 4)
a.due_date
else
a.start_date
end
[a.sort_order, sort_date]
end
example input -
[{name: item 1, start_date: "08/01/2016", due_date: "08/15/2016", sort_order: 3},
{name: item 2, start_date: "08/02/2016", due_date: "08/20/2016", sort_order: 3},
{name: item 3, start_date: "06/01/2016", due_date: nil, sort_order: 2},
{name: item 4, start_date: "06/01/2016", due_date: "07/15/2016", sort_order: 1},
{name: item 5, start_date: "07/01/2016", due_date: "07/07/2016", sort_order: 1},
{name: item 6, start_date: "01/01/2015", due_date: "01/15/2015", sort_order: 4},
{name: item 7, start_date: "01/01/2016", due_date: "01/15/2016", sort_order: 4},]
the order of expected output
item 5
item 4
item 3
item 1
item 2
item 7
item 6
Basically sort the assignments by current, future, past.
Upvotes: 0
Views: 147
Reputation: 36860
Just use negative values for the dates to sort descending.
@assignments.sort_by do |a|
case a.sort_order
when 1 then [1, a.due_date]
when 2 then [2, a.start_date]
when 3 then [3, a.start_date]
when 4 then [4, -a.due_date]
end
end
Upvotes: 2