Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

sort array of objects javascript by value

const subjects = [
    { "name": "Math", "score": 81 },
    { "name": "English", "score": 77 },
    { "name": "Chemistry", "score": 87 },
    { "name": "Physics", "score": 84 }
];

// Sort in ascending order - by name
subjects.sort((a, b) => (a.name > b.name) ? 1: -1);

console.log(subjects);
Comment

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));
Comment

JavaScript sort array of objects by property alphabetically

var data = [{ h_id: "3", city: "Dallas", state: "TX", zip: "75201", price: "162500" }, { h_id: "4", city: "Bevery Hills", state: "CA", zip: "90210", price: "319250" }, { h_id: "6", city: "Dallas", state: "TX", zip: "75000", price: "556699" }, { h_id: "5", city: "New York", state: "NY", zip: "00010", price: "962500" }];

data.sort(function (a, b) {
    return a.city.localeCompare(b.city) || b.price - a.price;
});

console.log(data);
Comment

javascript sort array of object by property

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

myDates.sort(sortByDate);//myDates is not sorted.
Comment

sort array of objects javascript by properties value

objs.sort((a,b) => (a.last_nom > b.last_nom) ? 1 : ((b.last_nom > a.last_nom) ? -1 : 0))
Comment

Sorting an array of objects by property values

let persolize=[ { key: 'speakers', view: 10 }, { key: 'test', view: 5 } ]
  
persolize.sort((a,b) => a.view - b.view);

//If it only array and not an array object, use below statement
//persolize.sort((a,b) => a - b);
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to find the smallest two numbers in an array javascript 
Javascript :: copy to clipboard jquery javascript 
Javascript :: regex to ignore white spaces js 
Javascript :: discord.js on ready 
Javascript :: moment get weekday name 
Javascript :: column.footer jquery 
Javascript :: generate random id javascript 
Javascript :: convert new date to minutes number javascript 
Javascript :: js input type range get value while sliding 
Javascript :: remove duplicates from array 
Javascript :: javascript update local storage array 
Javascript :: onloadscroll to top 
Javascript :: how to remove empty spaces befiore string js 
Javascript :: javascript date is an object 
Javascript :: javascript sort array by index 
Javascript :: multiple line string in jquery 
Javascript :: js get random hex color 
Javascript :: reactive forms change event in angular 
Javascript :: js clone element 
Javascript :: delete element 
Javascript :: import file json angular 12 
Javascript :: jquery $(document.on click 
Javascript :: string contains javascript 
Javascript :: moment time ago format reactjs 
Javascript :: chartjs min 
Javascript :: react-native run-ios command 
Javascript :: merge array within an array 
Javascript :: react-lottie yarn 
Javascript :: change alt text javascript 
Javascript :: swal change confirm button class 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =