From: Dan Zwell Date: 2007-08-27T06:30:33+09:00 Subject: Re: sort_by { rand } not working Mike Dershowitz wrote: > Hello: > > I've got an array of arrays that I'd like to sort_by random. each > individual array is just a hash of a value and then an object. Is there > some reason why sort_by { rand } wouldn't work? My code is simple (teams > is populated with a db call): > > @teams.each do |t| > @all << ["t",t] > end > #current not working > @all.sort_by { rand } > > Any ideas? Am I stretching the limit of the rand function? > > Thanks! > > Mike Mike, This has been discussed here before. Your two best choices are: @all = @all.sort_by {rand} or for i in 0...@all.length j = i+rand(@all.length-i) @all[i], @all[j] = @all[j], @all[i] end The second one will be faster, but you probably shouldn't care on small arrays. Dan