Javascript array sort
How to sort array or objects?
JS array sort
// Array of object
const cars = [
{make: 'Ford', model: 'Mondeo', gearbox: 'manual', year: 2016},
{make: 'VW', model: 'Passat', gearbox: 'automatic', year: 2014},
{make: 'Volvo', model: 'XC90', gearbox: 'automatic', year: 2012}
]
//Sort the cars by year, newest to oldest
const ordered = cars.sort(function(a, b) {
return a.year < b.year;
});
console.table(ordered);
ES6 syntax
const ordered_es6 = cars.sort((a, b) => (a.year < b.year));
console.log('ES6');
console.table(ordered_es6);
Check it here: http://codepen.io/mslepko/pen/BpJBqG?editors=0011