javascript sort array with objects
var array = [
{name: "John", age: 34},
{name: "Peter", age: 54},
{name: "Jake", age: 25}
];
array.sort(function(a, b) {
return a.age - b.age;
}); // Sort youngest first
javascript sort array with objects
var array = [
{name: "John", age: 34},
{name: "Peter", age: 54},
{name: "Jake", age: 25}
];
array.sort(function(a, b) {
return a.age - b.age;
}); // Sort youngest first
sort array of objects javascript by value
let orders = [
{
order: 'order 1', date: '2020/04/01_11:09:05'
},
{
order: 'order 2', date: '2020/04/01_10:29:35'
},
{
order: 'order 3', date: '2020/04/01_10:28:44'
}
];
console.log(orders);
orders.sort(function(a, b){
let dateA = a.date.toLowerCase();
let dateB = b.date.toLowerCase();
if (dateA < dateB)
{
return -1;
}
else if (dateA > dateB)
{
return 1;
}
return 0;
});
console.log(orders);
sort array elements in descending order based on object key
homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
javascript sort array of objects by key value ascending and descending order
function compareValues(key, order = 'asc') {
return function innerSort(a, b) {
if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
// property doesn't exist on either object
return 0;
}
const varA = (typeof a[key] === 'string')
? a[key].toUpperCase() : a[key];
const varB = (typeof b[key] === 'string')
? b[key].toUpperCase() : b[key];
let comparison = 0;
if (varA > varB) {
comparison = 1;
} else if (varA < varB) {
comparison = -1;
}
return (
(order === 'desc') ? (comparison * -1) : comparison
);
};
}
javascript sort object by value descending
javascript sort object by value descending
starWarsPeople.map(e=>{
return e
}).sort((a,b)=>{
if (a.height-b.height>1) {return -1;}
else if (a.height-b.height<1) {return 1;}
else {return 0;}
});
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us