DEV Community

loading...

Discussion on: Ruby Descending Order Method

Collapse
baweaver profile image
Brandon Weaver
def descending_order(n)
  n.digits.sort_by { |c| -c.ord }.join.to_i
end

This does a few interesting things:

  • It uses the Integer#digits method, which is effectively the same as int.to_s.chars or int.to_s.split('')
  • It uses the ord method to get the numeric representation of a character, but negates it to get back to an ascending order, much the same as one would do with a numeric list
  • join doesn't require '' as it'll assume that.

Granted there are likely better ways to go about it, but this gives some interesting ideas.

Collapse
chair profile image
chair Author

I like it thank you!