How can I sort an array in javascript with more than 2 objects?
In this code example, Mosh is sorting the array by its name (only id: 1 and id: 2 courses were provided). But what if we add a third course?
const courses = [
{id: 1, name: 'Node.js'},
{id: 2, name: 'javaScript'},
{id: 3, name: 'CSS'}
];
courses.sort(function(a, b) {
//a < b = -1;
//a > b = 1;
//a === b => 0;
let nameA = a.name.toUpperCase();
let nameB = b.name.toUpperCase();
if (nameA < nameB) return -1;
if (nameA > nameB) return 1;
return 0;
});
console.log(courses);
thank you!