# Ruby way to find in an Array the object with a maximun in an attribute?

**URL:** <https://www.ruby-forum.com/t/ruby-way-to-find-in-an-array-the-object-with-a-maximun-in-an-attribute/141551>\
**Category:** Ruby\
**Created:** [June 27, 2008, 12:18am UTC](https://www.ruby-forum.com/t/ruby-way-to-find-in-an-array-the-object-with-a-maximun-in-an-attribute/141551 "2008-06-27T00:18:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 27, 2008, 12:18am UTC](https://www.ruby-forum.com/t/ruby-way-to-find-in-an-array-the-object-with-a-maximun-in-an-attribute/141551/1 "2008-06-27T00:18:28Z")

</div>

Hi, I’ve an Array of objects containing some attributes:

class SRV

- @priority
- @domain
- @port  
end

In the Array I have 3 SRV objects:

Array[0]

- @priority = 1
- @domain = [mydomain.com](http://mydomain.com)
- @port = 5060

Array[1]

- @priority = 0
- @domain = [mydomain2.net](http://mydomain2.net)
- @port = 5060

Array[2]

- @priority = 0
- @domain = [mydomain3.org](http://mydomain3.org)
- @port = 5070

And I need to get an array containing the Array elements with smallest  
@priority value, this is:

ResultArray[0]

- @priority = 0
- @domain = [mydomain2.net](http://mydomain2.net)
- @port = 5060

ResultArray[1]

- @priority = 0
- @domain = [mydomain3.org](http://mydomain3.org)
- @port = 5070

Which is the best way to get the final array? Thanks for any suggestion.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 27, 2008, 12:36am UTC](https://www.ruby-forum.com/t/ruby-way-to-find-in-an-array-the-object-with-a-maximun-in-an-attribute/141551/2 "2008-06-27T00:36:03Z")

</div>

El Viernes, 27 de Junio de 2008, IÃ±aki Baz C. escribiÃ³:

> - @domain = [mydomain3.org](http://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) }

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 27, 2008, 1:38am UTC](https://www.ruby-forum.com/t/ruby-way-to-find-in-an-array-the-object-with-a-maximun-in-an-attribute/141551/3 "2008-06-27T01:38:48Z")

</div>

IÃ±aki Baz C. wrote:

> El Viernes, 27 de Junio de 2008, IÃ±aki Baz C. escribiÃ³:
> 
> > - @domain = [mydomain3.org](http://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
