How can I sort this array so all hashes with optional = true are last?
my_array = [
{ name: "foo", optional: true },
{ name: "pop" },
{ name: "boop", optional: true },
{ name: "pop", optional: false }
]
I tried this:
my_array.sort { |a,b| a['optional'] <=> b['optional'] }
But I get this error:
comparison of Hash with Hash failed
Error is surprising since doing a['optional'] would return a value of true, false, or nil.
Note:
I am aware of this solution .sort_by { |a| (a['optional'] ? 1 : 0) }. I would like to understand the sort method in particular.