Hi -- On Mon, 18 Jun 2007, Harry Kakueki wrote: > On 6/18/07, Gilbert Lau <gilbertlsk / gmail.com> 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 One way to generalize it: arr.sort.map {|c| myhash.keys.find {|k| myhash[k].include?(c) } } There's probably a more efficient way (there usually is when I'm the first to suggest these things :-) but that will get you the whole array. David -- * Books: RAILS ROUTING (new! http://safari.awprofessional.com/9780321509246) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)