Skip to content

Instantly share code, notes, and snippets.

@shammelburg
Created December 5, 2019 10:06
Show Gist options
  • Save shammelburg/6c9ab7922cc06f7c7c5ef7c570cffe5f to your computer and use it in GitHub Desktop.
Save shammelburg/6c9ab7922cc06f7c7c5ef7c570cffe5f to your computer and use it in GitHub Desktop.
JavaScript Sorting
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)
// 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