Created
February 26, 2017 08:56
-
-
Save Dosant/ef992245fbaee73a197c769e19e9176a to your computer and use it in GitHub Desktop.
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 article1 = { | |
id: 1, | |
createdAt: new Date('2012-04-04T24:00:00') | |
}; | |
var article2 = { | |
id: 1, | |
createdAt: new Date('2014-04-04T24:00:00') | |
} | |
var article3 = { | |
id: 1, | |
createdAt: new Date('2010-04-04T24:00:00') | |
} | |
var articles = [article1, article2, article3]; | |
console.log(articles); | |
articles.sort(function (a, b) { | |
if (a.createdAt - b.createdAt < 0) { | |
return 1; | |
} else { | |
return - 1; | |
} | |
}); | |
console.log(articles); // article3, article1, article2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment