sort_by is an Enumerable method for ordering a collection by a key computed from each element. Give it one block argument, and Ruby uses the returned value to compare elements while preserving the original objects in the result. section 10.8.2
Normalize before sorting
["2", 1, 5, "3", 4, "6"].sort_by { |item| item.to_i } # => [1, "2", "3", 4, 5, "6"] The block converts every item to an integer for comparison, but the result still contains the original strings and integers. section 10.8.1
Unlike sort with a comparison block, sort_by does not require two block parameters or an explicit <=> call. It applies the same key computation to each element, then orders the collection by those keys. section 10.8.1 section 10.8.2
The method belongs to Enumerable, so it is available through enumerable containers such as arrays. Sorting custom objects can therefore use a derived attribute such as price without requiring those objects themselves to mix in Enumerable. section 10.8 section 10.8.2
