From: Laurent Colloud Date: 2006-08-16T06:48:39+09:00 Subject: Re: Consecutive sort on Array of Hashes Hi guys, First thank you very much for helping me out on that one. So here are the results of all your suggestions. And the winner is... ----------------------------------- THEY WORK ----------------------------------- By James: myArray.sort_by { |team| [team[:pts], team[:w], team[:d]] }.reverse By Gennady (nice one!): myArray.sort { |b, a| diff = 0 [ :pts, :w, :d ].each do |_criteria| diff = a[_criteria] - b[_criteria] break unless diff.zero? end diff } By Gennady (nice one again!): myArray.sort { |a, b| [ :pts, :w, :d ].inject(0) { |_m, _c| _m == 0 ? b[_c] - a[_c] : _m } } By Russell (with a few synthax corrections): myArray.sort {|a,b| if b[:pts] == a[:pts] if b[:w] == a[:w] b[:d] <=> a[:d] else b[:w] <=> a[:w] end else b[:pts] <=> a[:pts] end } ----------------------------------------------- THEY DON'T WORK ----------------------------------------------- myArray.sort_by {|a| [a[:d], a[:w], a[:pts]]} -> Sorted by draws (:d) ASC (and then :w ASC, :pts ASC) but I could deduct James'solution from the output of this so thanks anyway :). myArray.sort {|a,b| (b[:d] <=> a[:d]).nonzero? || (b[:w] <=> a[:w]).nonzero? || (b[:pts] <=> a[:pts])} ->Sorted by :d DESC (and then :w DESC, :pts DESC) So thanks again very much. Pfiouuu, I've learned a lot of Ruby in just one night! :) -- Posted via http://www.ruby-forum.com/.