From: Timothy Hunter Date: 2006-01-04T22:22:58+09:00 Subject: Re: Newbie question about nested sort Grehom wrote: > what am I doing wrong? I wanted to sort primarily on count (second > position in array), then sub-sort alphabetically (first position in > array) > > char_freq = [["c", 2],["b", 5],["a", 2]] > sorted_freq = char_freq.sort {|a, b| b[1]<=>a[1] || a[0]<=>b[0] } > sorted_freq.each do |d| > print d[0]*d[1] > end > > produces >> bbbbbccaa > > when I was rather hoping for >> bbbbbaacc > Is this what you want? char_freq = [["c", 2],["b", 5],["a", 2]] sorted_freq = char_freq.sort_by {|a| [-a[1], a[0]] } sorted_freq.each do |d| print d[0]*d[1] end