From: Harry Kakueki Date: 2007-06-18T12:07:43+09:00 Subject: Re: Adding new value (in array) to existing key in a hash On 6/18/07, Gilbert Lau wrote: > Thanks a lot. Sorry, got another question > > let's say that the hash is > > myhash = {"a" => [[0,0],[3,4]], "b" => [[1,1],[9,8]], "c" => [[7,6]]} > > how do I sort the values to become > > [0,0],[1,1],[3,4],[7,6],[9,8] > > and be able to recall the keys to arrange according to the sorted > values, like > > "a", "b", "a", "c", "b" > Well, I think this will work. Extract the arrays from the values and then sort. myhash = {"a" => [[0,0],[3,4]], "b" => [[1,1],[9,8]], "c" => [[7,6]]} arr = [] myhash.each_value do |x| x.each {|y| arr << y} end p arr.sort! # This will find whether it is included in "a" for example # but you need to think about what to do if you have duplicates. # myhash = {"a" => [[0,0],[3,4]], "b" => [[1,1],[0,0],[9,8]], "c" => [[7,6]]} # This is ugly but maybe you can get some ideas arr.each do |c| p "FROM a" if myhash["a"].include?(c) p "NOT FROM a" unless myhash["a"].include?(c) end Harry -- A Look into Japanese Ruby List in English http://www.kakueki.com/