Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

array sort by key javascript

function sortByKey(array, key) {
  return array.sort((a, b) => {
    let x = a[key];
    let y = b[key];
    
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
  });
}
 
PREVIOUS NEXT
Tagged: #array #sort #key #javascript
ADD COMMENT
Topic
Name
7+6 =