advanced patterns When Sorting a List of Custom Objects by Multiple Attributes in Ruby
I'm working on a personal project and I'm working with an scenario while trying to sort a list of custom objects in Ruby by multiple attributes. I have a `Person` class with attributes `name`, `age`, and `city`. My intention is to sort a list of `Person` objects first by `age` in ascending order and then by `name` in alphabetical order if the ages are the same. However, the results are not as expected. Here's what I've implemented: ```ruby class Person attr_accessor :name, :age, :city def initialize(name, age, city) @name = name @age = age @city = city end end people = [ Person.new('Alice', 30, 'New York'), Person.new('Bob', 25, 'Los Angeles'), Person.new('Charlie', 25, 'Chicago'), Person.new('David', 30, 'San Francisco') ] sorted_people = people.sort_by { |person| [person.age, person.name] } ``` When I run the code above, I expect the first person in the sorted array to be Bob (age 25), followed by Charlie (age 25), then Alice (age 30), and finally David (age 30). However, I noticed that the order of Bob and Charlie does not reflect the alphabetical sorting. Instead, I get: ``` Bob Charlie Alice David ``` This seems to suggest that the first sort by `age` is working correctly, but the second sort by `name` is not. I also checked if there are any special characters in the names affecting the sort order, but they are all standard alphabetical characters. Am I missing something in the sorting logic, or is there a known bug in the Ruby version I'm using? I'm on Ruby 2.7.0. Any insights or suggestions would be greatly appreciated! My development environment is Windows. How would you solve this?