# Dot notation ordering

**URL:** <https://www.ruby-forum.com/t/dot-notation-ordering/79289>\
**Category:** Rails\
**Created:** [December 13, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/dot-notation-ordering/79289 "2006-12-13T22:11:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mephux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dfb087/32.png) [@mephux](https://www.ruby-forum.com/u/mephux)\
**Post date:** [December 13, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/dot-notation-ordering/79289/1 "2006-12-13T22:11:42Z")

</div>

How do I order a collection returned when I’ve accessed it from dot  
notation? For example using client.products I’d like the return to be  
ordered by product.part\_number. I’ve tried to do this Ruby side with  
something like client.products.sort {|a, b| a.part\_number \>  
b.part\_number} and I get an error. Any idea how to deal with this?

Thanks,  
-dustin

---

<div class="post-metadata">

**Author:** ![mephux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dfb087/32.png) [@mephux](https://www.ruby-forum.com/u/mephux)\
**Post date:** [December 13, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/dot-notation-ordering/79289/2 "2006-12-13T22:17:44Z")

</div>

On 12/13/06, Dustin W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How do I order a collection returned when I’ve accessed it from dot  
> notation? For example using client.products I’d like the return to be  
> ordered by product.part\_number. I’ve tried to do this Ruby side with  
> something like client.products.sort {|a, b| a.part\_number \>  
> b.part\_number} and I get an error. Any idea how to deal with this?

products = client.products.find( :all, :order =\> [‘part\_number’] )

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![mephux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dfb087/32.png) [@mephux](https://www.ruby-forum.com/u/mephux)\
**Post date:** [December 14, 2006, 1:40am UTC](https://www.ruby-forum.com/t/dot-notation-ordering/79289/3 "2006-12-14T01:40:23Z")

</div>

FYI Your sort code was almost right:  
@products = client.products.sort {|a, b| b.part\_number \<=\>  
a.part\_number}

would give you the products sorted high to low.

---

<div class="post-metadata">

**Author:** ![mephux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dfb087/32.png) [@mephux](https://www.ruby-forum.com/u/mephux)\
**Post date:** [December 14, 2006, 2:12am UTC](https://www.ruby-forum.com/t/dot-notation-ordering/79289/4 "2006-12-14T02:12:26Z")

</div>

Hi –

On Thu, 14 Dec 2006, jdswift wrote:

> > 
> 
> FYI Your sort code was almost right:  
> @products = client.products.sort {|a, b| b.part\_number \<=\>  
> a.part\_number}
> 
> would give you the products sorted high to low.

Or the slightly more concise:

@products = client.products.sort\_by {|prod| prod.part\_number }

David

–  
Q. What’s a good holiday present for the serious Rails developer?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
aka The Ruby book for Rails developers!  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![mephux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dfb087/32.png) [@mephux](https://www.ruby-forum.com/u/mephux)\
**Post date:** [December 14, 2006, 6:00am UTC](https://www.ruby-forum.com/t/dot-notation-ordering/79289/5 "2006-12-14T06:00:56Z")

</div>

On 12/13/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > ordered by product.part\_number. I’ve tried to do this Ruby side with  
> > > > would give you the products sorted high to low.
> 
> Or the slightly more concise:
> 
> @products = client.products.sort\_by {|prod| prod.part\_number }

Or the slightly more concise:

@products = client.products.sort\_by &:part\_number

😉

---

<div class="post-metadata">

**Author:** ![mephux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dfb087/32.png) [@mephux](https://www.ruby-forum.com/u/mephux)\
**Post date:** [December 14, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/dot-notation-ordering/79289/6 "2006-12-14T21:59:05Z")

</div>

On 12/13/06, Jeremy E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @products = client.products.sort\_by &:part\_number

Where is this behavior documented?

Thanks,

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![mephux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dfb087/32.png) [@mephux](https://www.ruby-forum.com/u/mephux)\
**Post date:** [December 14, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/dot-notation-ordering/79289/7 "2006-12-14T22:11:08Z")

</div>

On 12/14/06, Greg D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > @products = client.products.sort\_by &:part\_number
> 
> Where is this behavior documented?

Nevermind, I found it:

> cat /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/core\_ext/symbol.rb

class Symbol

# Turns the symbol into a simple proc, which is especially useful

for enumerations. Examples:

# 

# # The same as people.collect { |p| p.name }

# people.collect(&:name)

# 

# # The same as people.select { |p| p.manager? }.collect { |p|

p.salary }

# people.select(&:manager?).collect(&:salary)

def to\_proc  
Proc.new { |obj, \*args| obj.send(self, \*args) }  
end  
end

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![mephux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dfb087/32.png) [@mephux](https://www.ruby-forum.com/u/mephux)\
**Post date:** [December 15, 2006, 2:19am UTC](https://www.ruby-forum.com/t/dot-notation-ordering/79289/8 "2006-12-15T02:19:41Z")

</div>

Hi –

On Thu, 14 Dec 2006, Greg D. wrote:

> class Symbol  
> end  
> end

It’s also in the development version of Ruby (1.9):

$ /usr/local/lib/ruby-cvs/bin/ruby -ve ‘p %w{ a b c }.map(&:upcase)’  
ruby 1.9.0 (2006-12-09 patchlevel 0) [i686-linux]  
[“A”, “B”, “C”]

David

–  
Q. What’s a good holiday present for the serious Rails developer?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
aka The Ruby book for Rails developers!  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![mephux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dfb087/32.png) [@mephux](https://www.ruby-forum.com/u/mephux)\
**Post date:** [December 15, 2006, 2:05am UTC](https://www.ruby-forum.com/t/dot-notation-ordering/79289/9 "2006-12-15T02:05:40Z")

</div>

Excellent. Thank you all for the great answers…

-dustin
