Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Sort articles by highest stock balance. If two articles have same stock, sort by newest date
- * @param {JSON-Array} inputArray - JSON-Array to be sorted
- * @return {JSON-Array} - Sorted JSON-array
- */
- function get_sorted_articles(inputArray){
- // Making deep copy of inputArray, so to not mutate it
- let sortedArray = JSON.parse(JSON.stringify(inputArray));
- // Sort the array by highest stock balance, or by newest date if same balance
- sortedArray.sort(function(a, b) {
- // First compare stock balance. If it returns 0 (same balance), then compare date
- return b["inventory"] - a["inventory"] || new Date(b["creation_date"]) - new Date (a["creation_date"]);
- });
- return sortedArray;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement