Ruby

Ruby

Made by DeepSource

Replace sort, max and min with respective _by methods RB-PR1005

Performance
Major
Autofix

sort { |a, b| a.foo <=> b.foo } can be replaced by sort_by(&:foo). This is true for max and min methods, too.

All the sort, min and max methods with block in the following example can be replaced by the respective _by methods.

Bad practice

array.sort { |a, b| a.foo <=> b.foo }
array.max { |a, b| a.foo <=> b.foo }
array.min { |a, b| a.foo <=> b.foo }
array.sort { |a, b| a[:foo] <=> b[:foo] }

Recommended

array.sort_by(&:foo)
array.sort_by { |v| v.foo }
array.sort_by do |var|
  var.foo
end
array.max_by(&:foo)
array.min_by(&:foo)
array.sort_by { |a| a[:foo] }