Created
December 5, 2019 10:06
-
-
Save shammelburg/6c9ab7922cc06f7c7c5ef7c570cffe5f to your computer and use it in GitHub Desktop.
JavaScript Sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const asc = (a, b) => a - b | |
const desc = (a, b) => b - a | |
array = array.map(a => new Date(a.dob)).sort(desc)//.reverse() // Date | |
array = array.map(a => a.pocketMoney).sort(asc)//.reverse() // Number | |
console.log(array) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sorting an array of objects by a string value property | |
const sortString = (a,b, propertyName) => { | |
var x = a[propertyName].toLowerCase() | |
var y = b[propertyName].toLowerCase() | |
if (x < y) { return -1 } | |
if (x > y) { return 1 } | |
return 0 | |
} | |
array = array.sort((a,b) => sortString(a,b, 'name'))//.reverse() | |
console.log(array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment