How do you convert a Date in the form of a String to a ActiveSupport::TimeWithZone?
I am trying to create a stream that includes Twitter data + my app's but I'm having trouble sorting them because their time stamps are formatted in different ways. This is my code:
answers = Answer.find(:all, :conditions => {:user_id => @user_id }, :limit => 20)
tweets = Twitter::Search.new(params[:username]).to_a
@feed =开发者_如何学Go (answers + tweets).sort_by(&:created_at)
these are the formats on time:
<#Hashie::Mash created_at="Tue, 22 Jun 2010 04:41:23 +0000"...
<Answer id:... created_at: "2010-06-15 02:13:40"
Any help would be greatly appreciated.
I don't know if this is the best way, but try this:
@feed = (answers + tweets).sort_by{ |x| DateTime.parse("#{x.created_at}") }
Edit
Reverse:
@feed = (answers + tweets).sort{ |x, y| DateTime.parse("#{y.created_at}") <=> DateTime.parse("#{x.created_at}") }
精彩评论