just found a piece of the problem :
all_users.sort_by{|user| [user.send(''current_distance''),
user.name]}
seems to work , but how can I set the sort order ? DESC on
current_distance et ASC on name ????
thanks
On 12 sep, 18:18, Erwin <yves_duf...-ee4meeAH724@public.gmane.org>
wrote:> HI mates
>
> I am trying to sort an array on 2 criterias , one is just in memory
> accessor, other is a record attribute :
>
> I have the following User model :
>
> class User < ActiveRecord::Base
> # name is an attribute in the table
> ....
> attr_accessor :current_distance
> ..
> end
>
> In my controller, I calculate current_distance for each customer, and
> set user.current_distance , but don''t save the record...
>
> then get the following array :
> all_users =>
> [#<User id: 1, name: "Alan", created_at: "2008-09-11
14:30:38">,
> #<User id: 2, name: "Bill, created_at: "2008-09-11
14:30:38">,
> #<User id: 3, name: "John", created_at: "2008-09-11
14:30:38">,
> #<User id: 4, name: "Michael", created_at: "2008-09-11
14:30:38">]
>
> that I can sort easily using
> all_users.sort_by {|item| item.send ''current_distance'' }
>
> obviously, I cannot use all_users.sort_by {|user|
> user[:current_distance] } ... but I can use all_users.sort_by {|
> user| user[:name] }
>
> my issue is trying to sort_by both criteria 1st on
> current_distance ASC, and 2nd on name DESC
>
> any enlightment ?
>
> thanks for your help
>
> erwin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---