From: Glenn Jackman Date: 2010-03-26T00:30:08+09:00 Subject: Re: problem sorting At 2010-03-25 10:58AM, "Augusto Garcia" wrote: > I am trying to sort an array using a block but somehow I am not seeing > what I am want. First I want to sort the total, then break the tie on > gold goldcount, silvercount and finally on bronzcount > > Here is my code. what am I missing? please help > > # compare and order by totals > result = b[:total] <=> a[:total] > # if tied, break the number of gold medals > result != 0 ? result : b[:goldcount] <=> a[:goldcount] > result != 0 ? result : b[:silvercount] <=> a[:silvercount] > result != 0 ? result : b[:bronzcount] <=> a[:bronzcount] > result != 0 ? result : b[:country] <=> a[:country] You're not assigning the tiebreaker back to the 'result' variable # compare result = b[:total] <=> a[:total] || b[:goldcount] <=> a[:goldcount] || b[:silvercount] <=> a[:silvercount] || b[:bronzcount] <=> a[:bronzcount] || b[:country] <=> a[:country] It might be clearer to use sort_by instead of sort sorted = array.sort_by do |h| [ h[:total],h[:goldcount],h[:silvercount],h[:bronzecount],h[:country] ] end -- Glenn Jackman Write a wise saying and your name will live forever. -- Anonymous