Created
July 26, 2016 17:01
-
-
Save jadhavj/388d0b2c8b8707a959d7bd06a3c9580b to your computer and use it in GitHub Desktop.
Sort by number
This file contains hidden or 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
var arrayOfObjects = [ | |
{ | |
name: 'Diana', | |
born: 1373925600000, // Mon, Jul 15 2013 | |
num: 4, | |
sex: 'female' | |
}, | |
{ | |
name: 'Beyonce', | |
born: 1366832953000, // Wed, Apr 24 2013 | |
num: 2, | |
sex: 'female' | |
}, | |
{ | |
name: 'Albert', | |
born: 1370288700000, // Mon, Jun 3 2013 | |
num: 3, | |
sex: 'male' | |
}, | |
{ | |
name: 'Doris', | |
born: 1354412087000, // Sat, Dec 1 2012 | |
num: 1, | |
sex: 'female' | |
} | |
]; | |
// use slice() to copy the array and not just make a reference | |
var byDate = arrayOfObjects.slice(0); | |
byDate.sort(function(a,b) { | |
return a.born - b.born; | |
}); | |
console.log('by date:'); | |
console.log(byDate); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment