Question

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..

Was it helpful?

Solution

tag_hash = tag_hash.sort_by do |_, leaf|
  leaf.reject do |key, _|
    key == 4
  end.collect(&:last).inject(:+)
end

OTHER TIPS

my_hash.sort_by do |_, h|
  h.inject(0) do |sum, n|
    # only add to the sum if the key isn't '4'
    n[0] == 4 ? sum : (sum + n[1])
  end
end

This can, of course, be shortened to a hideously unreadable one-liner:

my_hash.sort_by {|k,h| h.inject(0) {|sum,n| n[0] == 4 ? sum : (sum + n[1]) } }

This

puts a.sort_by { |k, v| -v.values[0..-1].inject(&:+) }

produces the following output, was this what you wanted?

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

This seems to work:

x.sort_by{ |_,h| h.values.take(3).inject(:+) }

This assumes that the subhashes are sorted (the first three entries are the entries you want to sum up).

When you use ActiveSupport you could do this:

x.sort_by{ |_,h| h.values.take(3).sum }

or

x.sort_by{ |_,h| h.slice(1,2,3).values.sum }

Hash#slice returns a hash that contains only the keys passed to slice. Array#take returns an array containing the first n entries of the source array.

(Ruby 1.9.1 required i think)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top