Skip to content

Instantly share code, notes, and snippets.

@Dosant
Created February 26, 2017 08:56
Show Gist options
  • Save Dosant/ef992245fbaee73a197c769e19e9176a to your computer and use it in GitHub Desktop.
Save Dosant/ef992245fbaee73a197c769e19e9176a to your computer and use it in GitHub Desktop.
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