开发者

Most efficient way to do this array insertion

I have an array of objects sorted by created_at (which is of course a Time object)

I have a hash of objects which looks lik开发者_Go百科e

{
 time_in_integer1 => object1,
 time_in_integer2 => object2,
 time_in_integer3 => object3,
 .... 
}

I would like to insert object1, 2, 3 into the array in the most efficient manner, sorted by comparing the created_at with the keys in the hash.

What would be the best way to achieve this?


See http://www.ruby-forum.com/topic/134477#599091.


This should do it:

my_array = (my_array + my_hash.values).sort_by {|obj| obj.created_at }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜