1.
Assuming rank is numeric:
array.sort {| a, b | a[:rank] <=> b[:rank] }
This is basically just specifying that we compare a and b using [:rank]
.
2.
array.sort {| a, b | a[:rank] == b[:rank] ?
a[:user].created_at <=> b[:user].created_at :
a[:rank} <=> b[:rank] }
This uses a ternary. If the ranks are equal, we compare by [:user].created_at. Otherwise, we compare by the ranks.
You could implement <=> in your own class to allow sorting natively:
class Leader < Struct.new(:rank, :user)
def <=>(other)
self[:rank] <=> other[:rank]
end
end
Then you can do:
leaders.sort()
If you include Comparable, it will provide the other comparison operators too.