From e603842e7caccb977155f70ba5a140a7bfcab93c Mon Sep 17 00:00:00 2001 From: Artem Darius Weber Date: Wed, 8 Jan 2025 08:46:32 +0300 Subject: [PATCH] feat: add sorting examples for numbers, words, and people in sort_sort_by.rb --- sort_sort_by.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 sort_sort_by.rb diff --git a/sort_sort_by.rb b/sort_sort_by.rb new file mode 100644 index 0000000..5406435 --- /dev/null +++ b/sort_sort_by.rb @@ -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}" \ No newline at end of file