DekGenius.com
JAVASCRIPT
sort array of object js
const books = [
{id: 1, name: 'The Lord of the Rings'},
{id: 2, name: 'A Tale of Two Cities'},
{id: 3, name: 'Don Quixote'},
{id: 4, name: 'The Hobbit'}
]
books.sort((a,b) => (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0));
javascript sort in array of objects
// Price Low To High
array?.sort((a, b) => (a.price > b.price ? 1 : -1))
// Price High To Low
array?.sort((a, b) => (a.price > b.price ? -1 : 1))
// Name A to Z
array?.sort((a, b) => (a.name > b.name ? 1 : 1))
// Name Z to A
array?.sort((a, b) => (a.name > b.name ? -1 : 1))
// Sort by date
array.sort((a,b) => new Date(b.date) - new Date(a.date));
sort object by value javascript
const objSortedByVal = Object.fromEntries(
Object.entries(obj).sort(([, a], [, b]) => a - b)
)
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 with objects
const list = [
{ color: 'white', size: 'XXL' },
{ color: 'red', size: 'XL' },
{ color: 'black', size: 'M' }
]
var sortedArray = list.sort((a, b) => (a.color > b.color) ? 1 : -1)
// Result:
//sortedArray:
//{ color: 'black', size: 'M' }
//{ color: 'red', size: 'XL' }
//{ color: 'white', size: 'XXL' }
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);
sort array of objects javascript
list.sort((a, b) => (a.color > b.color) ? 1 : -1)
js sort array of objects
const drinks1 = [
{name: 'lemonade', price: 90},
{name: 'lime', price: 432},
{name: 'peach', price: 23}
];
function sortDrinkByPrice(drinks) {
return drinks.sort((a, b) => a.price - b.price);
}
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));
order array of objects by id javascript
items.sort(function(a, b) {
return a.id - b.id || a.name.localeCompare(b.name);
});
sort array of objects javascript
const books = [
{id: 1, name: 'The Lord of the Rings'},
{id: 2, name: 'A Tale of Two Cities'},
{id: 3, name: 'Don Quixote'},
{id: 4, name: 'The Hobbit'}
]
books.sort((a, b) => a.name.localeCompare(b.name))
sort object array javascript
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);
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.
sort array of objects javascript
//sort array of objects javascript
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
how to sort an array of object
function compareFn(a, b) {
if (a is less than b by some ordering criterion) {
return -1;
}
if (a is greater than b by the ordering criterion) {
return 1;
}
// a must be equal to b
return 0;
}
sort object array javascript
//using es6, simply:
data.sort((a, b) => a.city.localeCompare(b.city) || b.price - a.price);
sort() object values javascript
list.sort((a, b) => (a.color > b.color) ? 1 : (a.color === b.color) ? ((a.size > b.size) ? 1 : -1) : -1 )
sort array of objects
function compareAge(a, b) {
return a.age - b.age;
}
const students = [{name: 'Sara', age:2},{name: 'John', age:1}, {name: 'Jack', age:0}];
console.log(students.sort(compareAge));
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 }]
})();
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))
js sort array of objects
{datacategorie?.sort((a,b)=> a.titreScategorie?.toLowerCase() > b.titreScategorie?.toLowerCase() ? 1 : -1)?.map((item) => (
<option key={item.id} value={item.id}>
{item.titreScategorie}
</option>
))}
Sort an Array of Objects in JavaScript
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);
© 2022 Copyright:
DekGenius.com