user19408396
user19408396

Reputation:

Group by specific array value in hash and include key

I am trying to find out if it is possible to sort a Hash by a specific value if there are multiple values saved to a key.

Example Code:

{
key1: ["Value1", "Value2", "Value3"], 
key2: ["Value1", "Value2", "Value3"],
key3: ["Value1", "Value2", "Value3"]
}

I would like to be able to sort by the values in Value2 or by Value1 or any specific value.

If key1 and key2 have the same Value2 they will be returned as:

{Value2: [key1, key2]}

Example:

Value2 representes pets:

{cat: ["key1", "key2"], dog: ["key3"]}

I am working in Ruby and VScode. I do not want to use an array or nested array because this is not efficient.


Given

{
key1: ["Cop", "dog", "house"],
key2: ["doctor", "cat", "apartment"],
key3: ["Chef", "dog", "house"],
key4: ["Cop", "cat", "apartment"]
}

Expected Output if asked to sort by the values in value2

{
dog: [key1, key3],
cat: [key2, key4]
}

Upvotes: 1

Views: 93

Answers (3)

Christian
Christian

Reputation: 5521

Maybe not the optimal solution but it returns the expected result. The code is at least groups by value and includes the key, resulting in:

{"cat"=>[:key2, :key4], "dog"=>[:key1, :key3]}

hash = {
    key1: ["Cop", "dog", "house"],
    key2: ["doctor", "cat", "apartment"],
    key3: ["Chef", "dog", "house"],
    key4: ["Cop", "cat", "apartment"]
}

def group_by(animal, hash)
  keys = hash.select{|key, value| value[1] == animal }.keys
  {animal => keys}
end 

a = group_by("cat", hash)
b = group_by("dog", hash)
a.merge(b)

I am sure there is a more "direct" solution.

Upvotes: 1

Chris
Chris

Reputation: 36496

Using #each_with_object to build up a hash as we iterate over the keys in the original hash.

data = {
    key1: ["Cop", "dog", "house"],
    key2: ["doctor", "cat", "apartment"],
    key3: ["Chef", "dog", "house"],
    key4: ["Cop", "cat", "apartment"]
}

data.keys.each_with_object(Hash.new([])) { |k, h| 
  h[data[k][1]] += [k] 
}
# => {"dog"=>[:key1, :key3], "cat"=>[:key2, :key4]}

Upvotes: 1

Schwern
Schwern

Reputation: 164699

This doesn't appear to be about sorting, but rather grouping.

It's a matter of iterating through each pair and building a new hash of the matching keys and values.

# A Hash where the default value is a new empty array.
# See https://stackoverflow.com/questions/30367487/creating-a-hash-with-values-as-arrays-and-default-value-as-empty-array
grouped = Hash.new { |h, k| h[k] = [] }

# Iterate through the Hash
h.each { |key, things|
  # Get the 2nd thing
  thing = things[1]

  # Add its key to the thing's group
  grouped[thing] << key
}

p grouped

Upvotes: 2

Related Questions