红宝石 |数组排序()函数
Array#sort() : sort()是一个 Array 类方法,它返回一个通过自我排序创建的新数组
Syntax: Array.sort()
Parameter: Array
Return: a new array created by sorting self
示例 #1:
# Ruby code for sort() method
# declaring array
a = ["abc", "nil", "dog"]
# declaring array
c = ["cat", "efg", "geeks"]
# declaring array
b = ["cow", "coal", "dog"]
arr = [a, b, c]
# sort method example
puts "sort() method form : #{arr.sort()}\n\n"
输出 :
sort() method form : [["abc", "nil", "dog"], ["cat", "efg", "geeks"], ["cow", "coal", "dog"]]
示例 #2:
# Ruby code for sort() method
# declaring array
a = ["abc", "nil", "dog"]
# declaring array
c = ["cat", "efg", "geeks"]
# declaring array
b = ["cow", "coal", "dog"]
# sort method example
puts "sort() method form : #{a.sort()}\n\n"
puts "sort() method form : #{b.sort()}\n\n"
puts "sort() method form : #{c.sort()}\n\n"
输出 :
sort() method form : ["abc", "dog", "nil"]
sort() method form : ["coal", "cow", "dog"]
sort() method form : ["cat", "efg", "geeks"]