Currently I have a list of random games in an array.
games=[["Game1", 2014, "Creator1", "Creator2", "Creator3"],
["Game4", 2013, "Creator7", "Creator10"],
["Game2", 2014, "Creator1"],
["Game6", 2012, "Creator9"],
["Game3", 1990, "Creator2", "Creator11"],
["Game9", 2014, "Creator4"]]
#Which looks like this
Title, Year, Creators
Game1, 2014, Creator1, Creator2, Creator3
Game4, 2013, Creator7, Creator10
Game2, 2014, Creator1
Game6, 2012, Creator9
Game3, 1990, Creator2, Creator11
Game9, 2014, Creator4
I wish to sort the data in the following order
- Year
- Title
Into
Creator1 => Game1, Game2
Creator10 => Game4
Creator11 => Game3
Creator2 => Game3, Game1 #<- year: 1990, 2014
Creator3 => Game1
Creator4 => Game9
Creator7 => Game4
Currently, I have
def creator_list(games)
games.sort_by{|x|[-x[1].to_i,x[0]]}
.inject(Hash.new{|h,k|h[k]=Array.new}) {|h, rw|
rw[2].each do |a|
h[a] << rw[0]
end
h
}.to_a.sort_by{|x|x[0]}
end
This my best result so far. However, I wish to know if there are any other algorithm or another approach to improve this?