From: Siep Korteling Date: 2008-06-27T08:36:30+09:00 Subject: Re: Ruby way to find in an Array the object with a maximun in an attribute? I単aki Baz Castillo wrote: > El Viernes, 27 de Junio de 2008, I単aki Baz Castillo escribi坦: >> - @domain = mydomain3.org >> - @port = 5070 >> >> >> Which is the best way to get the final array? Thanks for any suggestion. > > Done :) > > Array.sort! { |a,b| (a.priority <=> b.priority) } Yes! "sort_by" is nice too. My code just gives the minima (using a struct, it should work with a class) #preparation Somestruct = Struct.new(:priority,:domain,:port) ar = [] 5.times do ar << Somestruct.new(rand(10),rand(10),rand(10)) end puts ar #let's go smallest = ar.min{|x,y,|x.priority<=>y.priority} minima = ar.select{ |element| element.priority==smallest.priority } # in ruby 1.9 the last 2 lines can be replaced (i think) by # minima = ar.min_by{|x|x.priority} puts "only minimal priority's:" puts minima -- Posted via http://www.ruby-forum.com/.