how-to sorting after group_by ?

3,009 views
Skip to first unread message

Fabio Chiarelli

unread,
Aug 3, 2011, 4:28:16 AM8/3/11
to rubyonra...@googlegroups.com
I tried this code:

voiplogs = where(['data > ? AND data < ?', start_date,
end_date]).group_by(&:cli)

it's works, but how add also sort by :cli

Tanks to all
Fabio

--
Posted via http://www.ruby-forum.com/.

Jim Ruther Nill

unread,
Aug 3, 2011, 4:59:05 AM8/3/11
to rubyonra...@googlegroups.com
On Wed, Aug 3, 2011 at 4:28 PM, Fabio Chiarelli <li...@ruby-forum.com> wrote:
I tried this code:

voiplogs = where(['data > ? AND data < ?', start_date,
end_date]).group_by(&:cli)


voiplogs = where(['data > ? AND data < ?', start_date,
end_date]).order('cli ASC').group_by(&:cli)
 
it's works, but how add also sort by :cli

Tanks to all
Fabio

--
Posted via http://www.ruby-forum.com/.

--
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 rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.




--
-------------------------------------------------------------
visit my blog at http://jimlabs.heroku.com

7stud --

unread,
Aug 3, 2011, 6:18:26 AM8/3/11
to rubyonra...@googlegroups.com
In ruby 1.9.2,

...group_by(&:cli).sort

Otherwise, you can't because hashes are unordered by nature. You can
sort the keys and then access the hash using the sorted keys. Or, you
can use ActiveSupport::OrderedHash, and try to figure out how to
initialize it when you have a Hash.

Reply all
Reply to author
Forward
0 new messages