This is an array of objects:
const movies = [
{
title: "Terminator",
genre: { _id: "5b21ca3eeb7f6fbccd471818", name: "Action" },
},
{
title: "Die Hard",
genre: { _id: "5b21ca3eeb7f6fbccd471818", name: "Action" },
},
{
title: "Get Out",
genre: { _id: "5b21ca3eeb7f6fbccd471820", name: "Thriller" },
},
]
I would like to sort them by title and genre.name.
I want to create 2 new arrays, one is sorted by title and the other one is sorted by genre.name.
So far I tried this, but it was clearly wrong:
const sortMovies = (item) => {
movies.sort((a, b) =>
a.item> b.item? 1 : b.item> a.item? -1 : 0
);
};
const sortTitle = sortMovies(title);
const sortGenre = sortMovies(genre.name);
Please help me find a solution to this. I really appreciate it!
a.itemtoa[item]- likewise forbtitlethengenre.name. Otherwise, the result looks weird :)titleand the other one is sorted bygenre.name. Sorry for the confusion.