7

I have retrieve from ajax query a news feed. In this object, there is a date in this format :

Wed, 22 May 2013 08:00:00 GMT

I would like to sort all objects by date. Is it possible to do this using Javascript ?

UPDATE

Using this piece of code it works fine !

array.sort(function(a,b){
var c = new Date(a.date);
var d = new Date(b.date);
return c-d;
});
8
  • What does your current code that isn't working look like? What have you tried? Commented May 22, 2013 at 11:01
  • Sure is this possible. How would you like to have it sorted? By weekday, by timezone or by month? :-D Commented May 22, 2013 at 11:02
  • What if you put "javascript array sort" to google.com and see what happens? Commented May 22, 2013 at 11:02
  • stackoverflow.com/questions/10123953/… Commented May 22, 2013 at 11:02
  • stackoverflow.com/questions/10123953/… Commented May 22, 2013 at 11:02

3 Answers 3

14

1) You can't sort objects. The order of the object's keys is arbitrary.

2) If you want to sort an array by date (and they are already date obects), do the following:

array.sort ( function (date1, date2){
     return date1 - date2
});

If you first need to convert them to date objects, do the following (following the data structure according to your comment below):

array.sort ( function (a, b){
       return new Date(a.pubDate) - new Date(b.pubDate);
});

Example

Sign up to request clarification or add additional context in comments.

4 Comments

In fact, i have an array of news Object which contains the following keys : title, description, url and pubdate. I would like to sort my array by date.
@pxrb66 Then you can compare the pubdates of your array elements function(a,b){new Date(a.pubDate)-new Date(b-pubDate)}.
@pxrb66 Feel free to either write an answer for yourself with the codesnippet you posted in your updated answer and accept it or accept my answer. In any way you should accept one answer to mark your problem as solved to the SO community.
You can find some useful answers to this topic here: Sort Javascript Object Array By Date
5

You may also use a underscore/lodash sortBy

Here's using underscore js to sort date:

 var log = [{date: '2016-01-16T05:23:38+00:00', other: 'sample'}, 
            {date: '2016-01-13T05:23:38+00:00',other: 'sample'}, 
            {date: '2016-01-15T11:23:38+00:00', other: 'sample'}];

  console.log(_.sortBy(log, 'date'));

1 Comment

This really saved me a lot of time. Simple and effective.
1
sorting dates ascending or descending
times = ["01-09-2013", "01-09-2013", "27-08-2013", "27-08-2013", "28-08-2013", "28-08-2013", "28-08-2013", "28-08-2013", "29-08-2013", "29-08-2013", "30-08-2013", "30-08-2013", "31-08-2013", "31-08-2013"]
function dmyOrdA(a,b){ return myDate(a) - myDate(b);}
function dmyOrdD(a,b){ return myDate(b) - myDate(a);}
function myDate(s){var a=s.split(/-|\//); return new Date(a[2],a[1]-1,a[0]);}

times.sort(dmyOrdA);
console.log(times)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.