Ruby中sort and sort_by的不同

2016-03-09  兔宝贝 

#sort_by should just take one block parameter, an item from the array, and sorts based on the result of the block. 
[1,2,3].sort_by { |x| x } => [1, 2, 3] 
[1,2,3].sort_by { |x| -x } => [3, 2, 1] 
[1,2,3].sort => [1, 2, 3]
 [1,2,3].sort.reverse => [3, 2, 1] 
[1,2,3].sort { |x, y| x <=> y } => [1, 2, 3]
 [1,2,3].sort { |x, y| y <=> x } => [3, 2, 1]
380°/3802 人阅读/0 条评论 发表评论

登录 后发表评论
兔宝贝
访客 2021
兔宝贝 的其他博文 更多