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

order array of objects by id javascript

items.sort(function(a, b) { 
  return a.id - b.id  ||  a.name.localeCompare(b.name);
});
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 object properties by value javascript

var maxSpeed = {
    car: 300, 
    bike: 60, 
    motorbike: 200, 
    airplane: 1000,
    helicopter: 400, 
    rocket: 8 * 60 * 60
};
var sortable = [];
for (var vehicle in maxSpeed) {
    sortable.push([vehicle, maxSpeed[vehicle]]);
}

sortable.sort(function(a, b) {
    return a[1] - b[1];
});

//[["bike", 60], ["motorbike", 200], ["car", 300],
//["helicopter", 400], ["airplane", 1000], ["rocket", 28800]]
Comment

js how to sort array by object value

// @ts-check

(function () {
  const cars = [
    { type: 'Volvo', year: 2016 },
    { type: 'Saab', year: 2001 },
    { type: 'BMW', year: 2010 },
  ];

  /**
   * @param {object[]} arr
   */
  function sortByValue(arr) {
    arr.sort(function (
      /** @type {{ year: number; }} */ a,
      /** @type {{ year: number; }} */ b
    ) {
      return a.year - b.year;
    });
    return arr;
  }
  console.log(sortByValue(cars)); // => [{ type: 'Saab', year: 2001 }, { type: 'BMW', year: 2010 },{ type: 'Volvo', year: 2016 }]
})();
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 :: .ignore file nodejs 
Javascript :: json decode list flutter 
Javascript :: date format in react js 
Javascript :: react check if mobile or desktop 
Javascript :: nodejs mysql insert object query 
Javascript :: reverse a word javascript 
Javascript :: js text word wrap 
Javascript :: bootstrap 5.1 3 tooltip not working 
Javascript :: js input type range get value on select 
Javascript :: json.stringify formatting 
Javascript :: vue event focus out 
Javascript :: get full month from date javascript 
Javascript :: how to validate the textbox using jquery 
Javascript :: jquery find children not working 
Javascript :: sort object by key value js 
Javascript :: javascript remove all spaces 
Javascript :: regex for non empty string 
Javascript :: react native load function each time visit screen 
Javascript :: javascript create element with class 
Javascript :: Rename files in a directory with node.js 
Javascript :: getting state in react-router-dom v6 
Javascript :: reverse string js 
Javascript :: check if string contains substring javascript 
Javascript :: datatable on page change 
Javascript :: url regex javascript 
Javascript :: useScreens() react native 
Javascript :: get time from date javascript 
Javascript :: calculus of finite differences calculator 
Javascript :: javascript run command 
Javascript :: nodejs array buffer to buffer 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =