Q:

Write a Ruby program to sort a given array of strings by length

0

Write a Ruby program to sort a given array of strings by length. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

arra1 = ['abcde', 'abdf', 'adeab', 'abdgeee', 'bdefa', 'abc', 'ab', 'a', 'bacdef']
print "Original array:\n"
print arra1
print "\nSorted array of strings by length\n"
arra1 = arra1.sort_by(&:length)
print arra1

Output:

Original array:
["abcde", "abdf", "adeab", "abdgeee", "bdefa", "abc", "ab", "a", "bacdef"]
Sorted array of strings by length
["a", "ab", "abc", "abdf", "abcde", "adeab", "bdefa", "bacdef", "abdgeee"]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now