Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting nested hash in ruby

Provided the following ruby hash:

{
    cat: {
        1: 2,
        2: 10,
        3: 11,
        4: 1
    },
    wings: {
        1: 3,
        2: 5,
        3: 7,
        4: 7
    },
    grimace: {
        1: 4,
        2: 5,
        3: 5,
        4: 1
    },
    stubborn: {
        1: 5,
        2: 3,
        3: 7,
        4: 5
    }
}

How can I sort the hash by the sum of 'leaf' excluding "4", for instance the value to compare for "cat" would be (2 + 10 + 11) = 23, the value for "wings" would be (3 + 5 + 7) = 15 so if I was comparing just those two they would be in the correct order, highest sum on top.

It is safe to assume that it will ALWAYS be {1: value, 2: value, 3: value, 4: value} as those are keys for constants I have defined.

It is also safe to assume that I will only ever want to exclude the key "4", and always use the keys "1", "2", and "3"

Based on Jordan's suggestion I got this to work:

  tag_hash = tag_hash.sort_by do |h| 
    h[1].inject(0) do |sum, n| 
      n[0] == 4 ? sum : sum + (n[1] || 0)
    end
  end

The results seem a bit off but it appears to be my code, once I confirm that I'll accept the answer, thanks Jordan!

I've updated my solution to use Wayne Conrad's idea, see my comment on his answer - Is it possible that its not carrying everything when it sorts, I linked to an image in my comment that shows the result of the actual sort in graph form.. seems odd to me..

like image 907
Rabbott Avatar asked May 10 '26 05:05

Rabbott


1 Answers

tag_hash = tag_hash.sort_by do |_, leaf|
  leaf.reject do |key, _|
    key == 4
  end.collect(&:last).inject(:+)
end
like image 120
Wayne Conrad Avatar answered May 12 '26 19:05

Wayne Conrad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!