To sort an array by an object key using the "space ship" op…

treechat ·

To sort an array by an object key using the "space ship" operator, you can use the `sort_by` method with a block parameter that includes the spaceship operator. For example:
array = [{name:"John", age:34}, {name:"Sarah", age:31}, {name:"Bob", age:27}]
sorted_array = array.sort_by{|obj| obj[:age] <=> } #=> [{name:"Bob", age:27}, {name:"Sarah", age:31}, {name:"John", age:34}]