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. unbuntu下安装qq

    由于Wine QQ一直没更新版本导致目前版本报版本过低无法使用,暂时先上UK官网的国际版Wine QQ,虽然功能没那么新,但稳定能用: 下载: 下载地址:http://www.ubuntukylin. ...

  2. 【C++基础】形参实参

    c++规定:一个函数的默认实参既可以在定义中,也可在声明中指定,但在一个文件(准确的说,是一个作用域)中只能为一个形参指定默认实参一次

  3. django 自定义日志配置

    如果不想使用 python 的 dictConfig 格式来配置 logger,可以制定自己的配置架构. LOGGING_CONFIG 配置定义了用来配置 django logger 的可调用函数,默 ...

  4. Cache系列:spring-cache简单三步快速应用ehcache3.x-jcache缓存(spring4.x)

    前言:本项目基于spring4.x构建,使用ehcache3.5.2和JCache(jsr107规范) 一.依赖 除了ehcache和cache-api外,注意引用spring-context-sup ...

  5. C++源码学习

    C/C++是最主要的编程语言.这里列出了50名优秀网站和网页清单,这些网站提供c/c++源代码.这份清单提供了源代码的链接以及它们的小说明.我已尽力包括最佳的C/C++源代码的网站.这不是一个完整的清 ...

  6. python-pprint打印函数

    #!/usr/bin/env python # -*- coding:utf-8 -*- import sys,pprint pprint.pprint(sys.path)

  7. ubuntu安装vnc,远程链接时出现灰屏,配置文档不对吗

    摘自:https://zhidao.baidu.com/question/1949169099296473348.html 1.在Ubuntu上首先需要安装vnc4server # apt-get i ...

  8. maven工程导入eclipse后报错

    原因:需要重新部署下tomcat环境 解决方式: 右击->属性:

  9. Gym - 101889E Enigma(数位填数+记忆化)

    https://cn.vjudge.net/problem/Gym-101889E 1??????????????????????????????? 2 10000000000000000000000 ...

  10. 计总与排名SUM和RANK函数

    准备一些数据: CREATE TABLE [dbo].[SalesPerformance]( ,) NOT NULL, ) NOT NULL, [OrderDate] [DATE] NULL, ,) ...