Skip to content
Advertisement

Sort one inside another

I have a class Person that contains names and hobbies. Then there’s a list that contains people.

  • person1 = Person(“MarySmith”, [“dancing”, “biking”, “cooking”])
  • person2 = …
  • people = [person1, person2,..]

I need to return a list of people sorted alphabetically by their name, and also sort their list of hobbies alphabetically.

This is what I have so far:

JavaScript

This is what I’m expecting to get:

JavaScript

I don’t get how to implement sorting for hobbies into this. No matter what I do I get an unsorted list of hobbies.

Advertisement

Answer

You didn’t give the implementation of Person, so it’s hard to give an exact answer. I assume the following.

JavaScript

You could also sort a Person‘s hobbies upon class creation.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement