简体   繁体   中英

How to sort in descending order in Ruby?

It seems that Ruby only sorts in ascending order.

For example, I assign the following array to a variable called 'books'

books = ['Why Communism doesn't work' , "Giants", "Mein Kampf" ]

To sort your books in descending order, is the following code correct?

books.sort! do |f,b|
  b<=>f
end

按降序排序的快速方法是使用reverse

books.sort.reverse

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM