Javascript Array sortObjects(key = 'name', direction = 'asc')

Description

Javascript Array sortObjects(key = 'name', direction = 'asc')


Array.prototype.sortObjects = (key = 'name', direction = 'asc') => {
    const a = [...this];/*from  w  w  w .  j  av a 2  s  .c o m*/
    a.sort((a, b) => (direction.toLowerCase() === 'desc' ? b[key] - a[key] : a[key] - b[key]));
};



PreviousNext

Related