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里,能看到很多加了感叹号的函数,都意味着对函数操作的对象进行了状态更改。
												

ruby 数组array 排序sort 和sort!的更多相关文章

  1. js中数组Array对象的方法sort()的应用

    一. sort()方法的介绍 //给一组数据排序 var arrNum = [12,1,9,23,56,100,88,66]; console.log("排序前的数组:"+arrN ...

  2. javascript 数组Array排序

    var numberAry = [9,9,10,8,7,80,33,55,22]; numberAry.sort(); /*输出:10,22,33,55,7,8,80,9,9 上面的代码没有按照数值的 ...

  3. 雷林鹏分享:Ruby 数组(Array)

    Ruby 数组(Array) Ruby 数组是任何对象的有序的.整数索引的集合.数组中的每个元素都与一个索引相关,并可通过索引进行获取. 数组的索引从 0 开始,这与 C 或 Java 中一样.一个负 ...

  4. 数组Array的一些方法

    数组对象属性和方法的概述:1> arr.push() 将参数添加至数组的末尾,返回的是新数组的长度2> arr.unshift() 将参数添加到数组的开头,返回新数组的长度3> ar ...

  5. Array 数组的排序 sort

    JavaScript实现多维数组.对象数组排序,其实用的就是原生的sort()方法,用于对数组的元素进行排序.sort() 方法用于对数组的元素进行排序.语法如下:arrayObject.sort(s ...

  6. php 多维数组 array sort 排序 :array_multisort

    1.参考链接: php简单实现多维数组排序的方法 参考二: 这个链接很好,可以直接看这个:PHP array_multisort—对多个数组或多维数组进行排序 2.案例一: //13: 最佳: pub ...

  7. JavaScript 使用 sort() 方法从数值上对数组进行排序

    使用 sort() 方法从数值上对数组进行排序. <html> <body> <script type="text/javascript"> f ...

  8. 41.把数组排成最小的数[Sort array to smallest value]

    [题目] 输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的一个.例如输入数组{3,32,  321},则输出这两个能排成的最小数字321323.请给出解决问题的算法,并证明该 ...

  9. js中的数组Array定义与sort方法使用示例

    Array的定义及sort方法使用示例 Array数组相当于java中的ArrayList  定义方法:  1:使用new Array(5  )创建数组 var ary = new Array(5): ...

随机推荐

  1. MySQL5.6中新增特性、不推荐使用的功能以及废弃的功能

    虽然已经使用MySQL5.6版本有一段时间了,但由于没有和之前的版本作过详细比较,所以对于哪些重要的或者不太重要的特性是在新版本中引入的,还有哪些特性已经或者将要从旧版本中移除的并没有一个十分全面的了 ...

  2. C++多态的实现条件

    #include <iostream> class Person{ public: virtual void say(){ std::cout<<"person&qu ...

  3. 杂项随记:gcc/objdump/section等

    gcc -g 如果不打开-g或者-ggdb(GDB专用)调试开关,GCC编译时不会加入调试信息,因为这会增大生成代码的体积.GCC采用了分级调试,通过在-g选项后附加数字1.2或3来指定在代码中加入调 ...

  4. Maven项目实战(1)

    一.maven的好处? 同样的项目使用maven工程来实现,它的项目源码很小: 1.依赖管理 就是对jar 包管理的过程 2.项目的一键构建 (编译-----测试----- 运行 --打包------ ...

  5. java——构造方法重载

    class Person { private String name ; private int age ; public Person() { } public Person(String n,in ...

  6. POJ-3617

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25616   Accepted: 6984 De ...

  7. Oracle&nbsp;11gr2的完全卸载

    Oracle 11gr2的完全卸载方式与前些版本有了改变,运行D:\app\Administrator\product\11.2.0\dbhome_1\deinstall的deinstall.bat批 ...

  8. Jquery.ajax 详细解释 通过Http请求加载远程数据

    首先请看一个Jquery.ajax的例子 $.ajax({ type: "GET", url: "/api/SearchApi/GetResults", dat ...

  9. swfupload原理总结

    1.引入js(js内动态添加上传的文件并提交表单) 2.后台处理(将图片保存) 3.调用另一个js修改前台图片的地址(改为新的图片地址)

  10. Newtonsoft.Json序列化字符串-格式化和时间格式问题

    最近C#中需要将实体进行json序列化,使用了Newtonsoft.Json public static void TestJson()        {            DataTable d ...