sort array of object js javascript sort in array of objects javascript sort array with objects sort array with objects sort array of objects javascript how to sort array of objects in javascript

The solution for “sort array of object js javascript sort in array of objects javascript sort array with objects sort array with objects sort array of objects javascript how to sort array of objects in javascript” can be found here. The following code will assist you in solving the problem.

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));// 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));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 firstconst 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’ }list.sort((a, b) => (a.color > b.color) ? 1 : -1)function compare( a, b ) {
if ( a.last_nom < b.last_nom ){ return -1; } if ( a.last_nom > b.last_nom ){
return 1;
}
return 0;
}

objs.sort( compare );

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in