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 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

sort obj by its property name

const sortByProperty = (array, propertyName) => {
    return array.sort(function (a, b) {
        if (a[propertyName] < b[propertyName]) {
            return - 1
        } else if (a[propertyName] > b[propertyName]) {
            return 1
        }
        return 0
    });
}
Comment

how to sort one element in property javascript

1
2
3
4
5
var employees=[]
employees[0]={name:"George", age:32, retiredate:"March 12, 2014"}
employees[1]={name:"Edward", age:17, retiredate:"June 2, 2023"}
employees[2]={name:"Christine", age:58, retiredate:"December 20, 2036"}
employees[3]={name:"Sarah", age:62, retiredate:"April 30, 2020"}
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 :: regex find first instace 
Javascript :: queryselectorall javascript images in list 
Javascript :: localstorage try catch 
Javascript :: discord.js v13 if dm 
Javascript :: js largura da tela 
Javascript :: javascript edit form value 
Javascript :: on click copy text 
Javascript :: shorthand if statment in js 
Javascript :: floating button react 
Javascript :: javascript string normalize method 
Javascript :: get value from textbox in vanilla javascript 
Javascript :: regular expression characters 
Javascript :: shadowcolor liners in react native 
Javascript :: js create array with default value 
Javascript :: js loading spinner 
Javascript :: array every javascript 
Javascript :: element clicked js 
Javascript :: how to export a constant in javascript 
Javascript :: upload image to firebase 
Javascript :: variable for every user discord.js 
Javascript :: how to repeat an array of objects n times in javascript 
Javascript :: javascript escape regex 
Javascript :: check if all values in array are negative javascript 
Javascript :: javascript return object property from array 
Javascript :: js split string every n characters 
Javascript :: convert array to object 
Javascript :: javascript append array to array 
Javascript :: how to change background image dynamically in react 
Javascript :: remove object in array javascript 
Javascript :: get current url react router 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =