Skip to content

Order By Before Where (ARCH6076)

Key ruby.performance.orderby-before-where · Severity warning · Category performance

Sorting before filtering in a collection chain sorts a larger set than needed. Filter first, then sort.

def top_active(users)
users.sort_by(&:created_at)
.select(&:active?)
.first(10)
end
def top_active(users)
users.select(&:active?)
.sort_by(&:created_at)
.first(10)
end

Apply the filter before the sort in the chain so a smaller set is sorted.