ruby 数组array 排序sort 和sort!
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.
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!的更多相关文章
- js中数组Array对象的方法sort()的应用
一. sort()方法的介绍 //给一组数据排序 var arrNum = [12,1,9,23,56,100,88,66]; console.log("排序前的数组:"+arrN ...
- javascript 数组Array排序
var numberAry = [9,9,10,8,7,80,33,55,22]; numberAry.sort(); /*输出:10,22,33,55,7,8,80,9,9 上面的代码没有按照数值的 ...
- 雷林鹏分享:Ruby 数组(Array)
Ruby 数组(Array) Ruby 数组是任何对象的有序的.整数索引的集合.数组中的每个元素都与一个索引相关,并可通过索引进行获取. 数组的索引从 0 开始,这与 C 或 Java 中一样.一个负 ...
- 数组Array的一些方法
数组对象属性和方法的概述:1> arr.push() 将参数添加至数组的末尾,返回的是新数组的长度2> arr.unshift() 将参数添加到数组的开头,返回新数组的长度3> ar ...
- Array 数组的排序 sort
JavaScript实现多维数组.对象数组排序,其实用的就是原生的sort()方法,用于对数组的元素进行排序.sort() 方法用于对数组的元素进行排序.语法如下:arrayObject.sort(s ...
- php 多维数组 array sort 排序 :array_multisort
1.参考链接: php简单实现多维数组排序的方法 参考二: 这个链接很好,可以直接看这个:PHP array_multisort—对多个数组或多维数组进行排序 2.案例一: //13: 最佳: pub ...
- JavaScript 使用 sort() 方法从数值上对数组进行排序
使用 sort() 方法从数值上对数组进行排序. <html> <body> <script type="text/javascript"> f ...
- 41.把数组排成最小的数[Sort array to smallest value]
[题目] 输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的一个.例如输入数组{3,32, 321},则输出这两个能排成的最小数字321323.请给出解决问题的算法,并证明该 ...
- js中的数组Array定义与sort方法使用示例
Array的定义及sort方法使用示例 Array数组相当于java中的ArrayList 定义方法: 1:使用new Array(5 )创建数组 var ary = new Array(5): ...
随机推荐
- 1072 Gas Station (30)(30 分)
A gas station has to be built at such a location that the minimum distance between the station and a ...
- springMVC源代码阅读之servlet部分<一>servlet部分详解
[一]servlet的概念
- 「LuoguP1429」 平面最近点对(加强版)
题目描述 给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的 输入输出格式 输入格式: 第一行:n:2≤n≤200000 接下来n行:每行两个实数:x y, ...
- ACM学习历程—ZOJ3878 Convert QWERTY to Dvorak(Hash && 模拟)
Description Edward, a poor copy typist, is a user of the Dvorak Layout. But now he has only a QWERTY ...
- Chrome检查更新总失败?安装细则讲解
现在 Google Chrome 的稳定版都已经发布 68.0 版本了,我机上还是 54, 本想在线更新一下,结果点击菜单项中的“关于 Google Chrome”后,进入的界面提示“更新失败(错误: ...
- Btree算法的C语言实现
btree.h //实现对order序(阶)的B-TREE结构基本操作的封装. //查找:search,插入:insert,删除:remove. //创建:create,销毁:destory,打印:p ...
- 批处理中格式化Date
@Echo Off Set _Date=%date% If "%_Date%A" LSS "A" (Set _NumTok=1-3) Else (Set _Nu ...
- [cf2015ICLFinalsDiv1J]Ceizenpok’s formula
题意:$C_n^m\% k$ 解题关键:扩展lucas+中国剩余定理裸题 #include<algorithm> #include<iostream> #include< ...
- CF-811A
A. Vladik and Courtesy time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- SVN 被防火墙阻止的解决方法
SVN 被防火墙阻止的解决方法: 1. 进入WIN7的防火墙,看到有的SVN服务是被阻止的,专用的和公用的要设置为允许被防火墙阻止的解决方法" TITLE="SVN 被防火墙阻止的 ...