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