You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kubsu-sm5-ruby/sort_sort_by.rb

30 lines
993 B

numbers = [5, 3, 8, 1, 2]
# сортировка чисел
sorted_numbers = numbers.sort
puts "Sorted numbers using sort: #{sorted_numbers}"
# сортировка по алфавиту
words = ["apple", "orange", "banana", "grape"]
sorted_words = words.sort
puts "Sorted words using sort: #{sorted_words}"
people = [
{ name: "Alice", age: 30 },
{ name: "Bob", age: 25 },
{ name: "Charlie", age: 35 }
]
# сортировка по возрасту
sorted_people_by_age = people.sort { |a, b| a[:age] <=> b[:age] }
puts "Sorted people by age using sort: #{sorted_people_by_age}"
# сортировка по возрасту
sorted_people_by_age2 = people.sort_by { |person| person[:age] }
puts "Sorted people by age using sort_by: #{sorted_people_by_age2}"
# сортировка по длине строки
words = ["cat", "elephant", "dog", "hippopotamus"]
sorted_by_length = words.sort_by { |word| word.length }
puts "Sorted words by length using sort_by: #{sorted_by_length}"