js comment trier le tableau par valeur d'objet
// @ts-check
(function () {
const cars = [
{ type: 'Volvo', year: 2016 },
{ type: 'Saab', year: 2001 },
{ type: 'BMW', year: 2010 },
];
/**
* @param {object[]} arr
*/
function sortByValue(arr) {
arr.sort(function (
/** @type {{ year: number; }} */ a,
/** @type {{ year: number; }} */ b
) {
return a.year - b.year;
});
return arr;
}
console.log(sortByValue(cars)); // => [{ type: 'Saab', year: 2001 }, { type: 'BMW', year: 2010 },{ type: 'Volvo', year: 2016 }]
})();
Condemned Corncrake