「Ruby」配列の要素を降順にソートするサンプル
書式
配列名.sort!.reverse!
使用例
cftA=[32, 12, 66, 83, 22, 12] p "配列の要素を降順1:" p cftA.sort!.reverse! cftB=["fde", "hde1", "m23", "d33", "n44", "yy555"] p "配列の要素を降順2:" p cftB.sort!.reverse!
実行結果
>ruby test2020.rb
“配列の要素を降順1:"
[83, 66, 32, 22, 12, 12]
“配列の要素を降順2:"
[“yy555", “n44", “m23", “hde1", “fde", “d33"]