1.
sort → new_ary click to toggle source
sort { |a, b| block } → new_ary

Returns a new array created by sorting self.

Comparisons for the sort will be done using the <=> operator or using an optional code block.

The block must implement a comparison between a and b, and return -1, when a follows b, 0 when a and b are equivalent, or +1 if b follows a.

See also Enumerable#sort_by.

a = [ "d", "a", "e", "c", "b" ]
a.sort #=> ["a", "b", "c", "d", "e"]
a.sort { |x,y| y <=> x } #=> ["e", "d", "c", "b", "a"]
a.sort { |x,y| x <=> Y }  #=> ["a", "b", "c", "d", "e"]

2.
sort! → ary click to toggle source
sort! { |a, b| block } → ary

Sorts self in place.

Comparisons for the sort will be done using the <=> operator or using an optional code block.

The block must implement a comparison between a and b, and return -1, when a follows b, 0 when a and b are equivalent, or +1 if b follows a.

See also Enumerable#sort_by.

a = [ "d", "a", "e", "c", "b" ]
a.sort! #=> ["a", "b", "c", "d", "e"]
a.sort! { |x,y| y <=> x } #=> ["e", "d", "c", "b", "a"] 参考链接:http://www.ruby-doc.org/core-2.0/Array.html#method-i-sort 3.sort和sort!的区别: sort和sort!函数,默认都使用 <=>比较,他们的区别在于:
sort! 可能会改变原先的数组,所以加个感叹号提醒
sort 返回的是新数组,没对原先的数组进行修改
在ruby的SDK里,能看到很多加了感叹号的函数,都意味着对函数操作的对象进行了状态更改。
												

最新文章

  1. yum使用点滴
  2. Freemark基本语法知识(转)
  3. mysql处理高并发,防止库存超卖
  4. UA模拟
  5. HDU 4793 Collision --解方程
  6. ceph
  7. Kafka的coordinator
  8. Linux下ps命令
  9. contenteditable 属性
  10. JQuery实现两侧浮动广告
  11. Rsync的工作方式
  12. GPIO的配置过程
  13. week1总结
  14. 【HDFS API编程】从本地拷贝文件,从本地拷贝大文件,拷贝HDFS文件到本地
  15. gson格式化参数 对象转Map
  16. 微信支付自带的简易log
  17. 浅谈JAVA中HashMap、ArrayList、StringBuilder等的扩容机制
  18. 多线程-interrupt(),isInterrupted(),interrupted()(转)
  19. vue教程2-07 自定义指令
  20. CSS3 :nth-child(n)使用注意

热门文章

  1. 华为USG6500系列
  2. poj1475 Pushing Boxes[双重BFS(毒瘤搜索题)]
  3. 使用Netty实现的一个简单HTTP服务器
  4. SpringMVC之四:渲染Web视图
  5. mysql create table
  6. day8 服务器
  7. VNC协议分析
  8. 2. DVWA亲测文件包含漏洞
  9. Laravel框架中使用邮件发送功能
  10. 【转】ssm整合