I have this array of objects:
var obj = [
{name: Andrew, age: 19, favouriteCream: Vanilla},
{name: Carl, age:20, favouriteCream:Chocolate},
{name: Bob, age: 22, favouriteCream:Strawberry}
];
I am curious how to build a function to print out the array sorted by either name, age or favouriteCream. Was thinking about prompting a message where the user can input a word such as name, age, or cream (cream = favourite cream) and then have a function printSorted(sortedBy) {} where sortedBy would be the user's input, but can't get my head around it. Would it be possible to use a for loop get the index of each element and then use something like obj[idx].name or obj[idx].favouriteCream to sort the list with it?
Edit: If sorted by name, would be alphabetically, if sorted by age, either ascending or descending.
PS: I am extremely new to programming! Thanks in advance!