# The old sorting ip addresses problem

**URL:** <https://www.ruby-forum.com/t/the-old-sorting-ip-addresses-problem/144473>\
**Category:** Ruby\
**Created:** [July 29, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/the-old-sorting-ip-addresses-problem/144473 "2008-07-29T23:20:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![matu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@matu](https://www.ruby-forum.com/u/matu)\
**Post date:** [July 29, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/the-old-sorting-ip-addresses-problem/144473/1 "2008-07-29T23:20:34Z")

</div>

i am sure there has to be some ruby way of doing this in a really simple  
way…  
i came up with this, but i am sure there is some other way that is much  
cleaner.

> > a = [“5.100.200.50”, “10.150.50.1”, “10.150.50.2”, “10.150.51.1”, “19.18.16.15”, “192.168.0.1”]  
> > =\> [“5.100.200.50”, “10.150.50.1”, “10.150.50.2”, “10.150.51.1”,  
> > “19.18.16.15”, “192.168.0.1”]
> 
> > a.sort {|a1, a2| y = [a1, a2].map {|addr| x = 0; addr.split(’.’).reverse.each\_with\_index {|n, i| x = x + n.to\_i \* (256 \*\* (i))}; x}; y.last \<=\> y.first}  
> > =\> [“192.168.0.1”, “19.18.16.15”, “10.150.51.1”, “10.150.50.2”,  
> > “10.150.50.1”, “5.100.200.50”]

---

<div class="post-metadata">

**Author:** ![matu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@matu](https://www.ruby-forum.com/u/matu)\
**Post date:** [July 29, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/the-old-sorting-ip-addresses-problem/144473/2 "2008-07-29T23:27:36Z")

</div>

On Tue, Jul 29, 2008 at 2:19 PM, matu [m@t.u](mailto:m@t.u) wrote:

> i am sure there has to be some ruby way of doing this in a really simple way…  
> i came up with this, but i am sure there is some other way that is much cleaner.
> 
> > > a = [“5.100.200.50”, “10.150.50.1”, “10.150.50.2”, “10.150.51.1”, “19.18.16.15”, “192.168.0.1”]  
> > > =\> [“5.100.200.50”, “10.150.50.1”, “10.150.50.2”, “10.150.51.1”, “19.18.16.15”, “192.168.0.1”]  
> > > a.sort {|a1, a2| y = [a1, a2].map {|addr| x = 0; addr.split(‘.’).reverse.each\_with\_index {|n, i| x = x + n.to\_i \* (256 \*\* (i))}; x}; y.last \<=\> y.first}  
> > > =\> [“192.168.0.1”, “19.18.16.15”, “10.150.51.1”, “10.150.50.2”, “10.150.50.1”, “5.100.200.50”]

a.sort\_by {|x| x.split(/./).map {|i| i.to\_i}}.reverse

=\> [“192.168.0.1”, “19.18.16.15”, “10.150.51.1”, “10.150.50.2”,  
“10.150.50.1”, “5.100.200.50”]

This relies on the fact that ruby has built-in array sorting, where  
it’ll sort by the first element, then break ties on the second  
element, and so on, and a built-in sort\_by function, that transforms a  
collection according to the block given, and sorts by the transformed  
values.

martin

---

<div class="post-metadata">

**Author:** ![matu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@matu](https://www.ruby-forum.com/u/matu)\
**Post date:** [July 29, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/the-old-sorting-ip-addresses-problem/144473/3 "2008-07-29T23:35:47Z")

</div>

Martin DeMello wrote:

> 

thanks. this is much cleaner.

---

<div class="post-metadata">

**Author:** ![matu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@matu](https://www.ruby-forum.com/u/matu)\
**Post date:** [July 31, 2008, 2:07am UTC](https://www.ruby-forum.com/t/the-old-sorting-ip-addresses-problem/144473/4 "2008-07-31T02:07:33Z")

</div>

On Jul 29, 2008, at 14:19 PM, matu wrote:

> > > addr.split(’.’).reverse.each\_with\_index {|n, i| x = x + n.to\_i \*  
> > > (256 \*\* (i))}; x}; y.last \<=\> y.first}  
> > > =\> [“192.168.0.1”, “19.18.16.15”, “10.150.51.1”, “10.150.50.2”,  
> > > “10.150.50.1”, “5.100.200.50”]

$ ruby -ripaddr -e ‘puts [“5.100.200.50”, “10.150.50.1”,  
“10.150.50.2”, “10.150.51.1”, “19.18.16.15”, “192.168.0.1”].  
map { |ip| IPAddr.new ip }.sort\_by { |ip| ip.hton }.join(", ")’  
5.100.200.50, 10.150.50.1, 10.150.50.2, 10.150.51.1, 19.18.16.15,  
192.168.0.1
