I have a 2d array which is supposed to be a user ranking, based on points. I first init it:
ranked_user = []
Then I do some points calculations and push some stuff into the array:
ranked_user.push([user.id, user.username, user.location, points])
which results in
=> [[8, "Jhonny", "Berlin", 11], [9, "Ben", "Hamburg", 3], [10, "Hugo", "Munich", 6]]
now I want to sort that array based on the 4th value (points) in order to show the ranking. I've tried two things:
ranked_user.sort_by{|k|k[3]}
and
ranked_user.sort { |a, b| b[3] <=> a[3] }
I expect this:
=> [[9, "Ben", "Hamburg", 3], [10, "Hugo", "Munich", 6], [8, "Jhonny", "Berlin", 11]]
but the array simply is not sorted.
What do I do wrong?
sort_by!