feat: add sorting examples for numbers, words, and people in sort_sort_by.rb

lab3
Artem-Darius Weber 1 day ago
parent d7c500f202
commit e603842e7c

@ -0,0 +1,30 @@
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}"
Loading…
Cancel
Save