E
E
eldar_web2016-06-23 09:22:37
Ruby on Rails
eldar_web, 2016-06-23 09:22:37

How in Ruby, having such a hash, sort by condition?

For example, there is a hash like this:

list { :plan => {1000, 3000, 100}, :present => {530, 700,85 }}

You need to sort like this - the closer the present number is to the plan number, the higher it should be.
In our case, this should be the answer (in order):
list { :plan => {100, 1000, 3000}, :present => {85, 530, 700 }}

How can you do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Demidenko, 2016-06-24
@eldar_web

In your every question, the logic of hashes was initially laid down, which is why you suffer.

arr = [ { plan: 1000, present: 530 }, { plan: 3000, present: 700 }, { plan: 100, present: 85 } ]
arr.sort_by {|v| v[:plan] - v[:present] }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question