How to sort array of strings by length in Ruby

1 Answer

0 votes
arr = ["ruby", "c++", "python", "c#"]

arr = arr.sort {|left, right| left.length <=> right.length}

puts arr



# run:
#
# c#
# c++
# ruby
# python
#

 



answered Nov 7, 2020 by avibootz
...