javascript sortBy array of objects code example
Example 1: 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.
Example 2: sort arrays according to first array js
itemsArray.sort(function(a, b){
return sortingArr.indexOf(a) - sortingArr.indexOf(b);
});