Answers for "how to sort an object array using a particular property javascript and create a new array with the property"

1

how to sort an array of objects by a property value in javascript

let x = list.sort((a, b) => (a.name > b.name ? 1 : -1));
Posted by: Guest on June-06-2021
7

sort array of objects by string property value

function compareFirstNames( a, b ) {
  if ( a.first_name < b.first_name ){
    return -1;
  }
  if ( a.first_name > b.first_name ){
    return 1;
  }
  return 0;
}

var people =[
    {"first_name":"Carol", "age":29},
    {"first_name":"Anna", "age":32},
    {"first_name":"Bob", "age":32}
];

people.sort( compareFirstNames ); //people is now sorted by first name from a-z
Posted by: Guest on July-22-2019

Code answers related to "how to sort an object array using a particular property javascript and create a new array with the property"

Code answers related to "Javascript"

Browse Popular Code Answers by Language