instance method
in_order_of
Ruby on Rails edge
Since v7.0.10Signature
in_order_of(key, series, filter: true)
Returns a new Array where the order has been set to that provided in the series, based on the key of the objects in the original enumerable.
[ Person.find(5), Person.find(3), Person.find(1) ].in_order_of(:id, [ 1, 5, 3 ])
# => [ Person.find(1), Person.find(5), Person.find(3) ]
If the series include keys that have no corresponding element in the Enumerable, these are ignored. If the Enumerable has additional elements that aren’t named in the series, these are not included in the result, unless the filter option is set to false.
Parameters
-
keyreq -
seriesreq -
filterkey = true
Source
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 222
def in_order_of(key, series, filter: true)
if filter
group_by(&key).values_at(*series).compact.flatten(1)
else
sort_by { |v| series.index(v.public_send(key)) || series.size }
end
end
Defined in activesupport/lib/active_support/core_ext/enumerable.rb line 222
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in Enumerable