5

I have an array of objects that have a 'date' string property. ie:

[
    {
        id: 1,
        startDate: '2011-4-22'
    },
    {
        id: 2,
        startDate: '2012-3-15'
    },
    {
        id: 3,
        startDate: '2011-4-22'
    },
    {
        id: 4,
        startDate: '2012-2-10'
    }
]

I just want to convert the date strings to a date and sort them by startDate DESC. Can someone please tell me how to do this with teh underscore.js _sortBy method or even just plain javascript will do.

Thanks!

29er
  • 8,595
  • 12
  • 48
  • 65

4 Answers4

15

An Underscore solution could look like this:

a = [ /* ... */ ];

function to_date(o) {
    var parts = o.startDate.split('-');
    o.startDate = new Date(parts[0], parts[1] - 1, parts[2]);
    return o;
}
function desc_start_time(o) {
    return -o.startDate.getTime();
}
var b = _.chain(a)
         .map(to_date)
         .sortBy(desc_start_time)
         .value();

You don't have to use named functions of course but the names do make the logic a bit clearer.

Demo: http://jsfiddle.net/ambiguous/qe9sZ/

In plain JavaScript you could do it like this:

for(var i = 0, parts; i < a.length; ++i) {
    parts = a[i].startDate.split('-');
    a[i].startDate = new Date(parts[0], parts[1] - 1, parts[2]);
}
var b = a.sort(function(a, b) {
    return b.startDate - a.startDate;
});

Demo: http://jsfiddle.net/ambiguous/rPAPG/

mu is too short
  • 426,620
  • 70
  • 833
  • 800
4

forEach and sort should handle that for you:

var data = [
    {
        id: 1,
        startDate: '2011-4-22'
    },
    {
        id: 2,
        startDate: '2012-3-15'
    },
    {
        id: 3,
        startDate: '2011-4-22'
    },
    {
        id: 4,
        startDate: '2012-2-10'
    }
];

var i, c;

for(i = 0; c = data[i]; i++) {
    var parts = c.startDate.split('-');

    c.startDate = new Date(+parts[0], +parts[1] - 1, +parts[2]);
}

data.sort(function(a, b) {
    return b.startDate - a.startDate;
});

Here's a demo; check your console.

Ry-
  • 218,210
  • 55
  • 464
  • 476
0

I did it this way:

 var sorted = _(list).sortBy(
                    function (item) {                        
                        return [new Date(item.effectiveDate).getTime(), item.batchId];
                    }), "batchId");

If you want it descending then it's the same thing but *-1

 var sorted = _(list).sortBy(
                    function (item) {                        
                        return [new Date(item.effectiveDate).getTime()*-1, item.batchId];
                    }), "batchId");

In this example I am ordering by two fields, you can forget about the item.batchId.

Hope this helps someone.

Sanchitos
  • 8,423
  • 6
  • 52
  • 52
0

If you are fetching datetime field from database then you can convert the datetime to timestamp and then sort. And then reverse the array.

const _ = require('underscore');

var object = [{title:"a", date:"2018-03-22T09:10:21.000Z"}, {title:"b", date:"2018-08-22T09:10:21.000Z"}, {title:"c", date:"2018-04-22T09:10:21.000Z"}];

withTimeStamp = _.map(object, function(val, key){ 
  val.timestamp = new Date(val.date).getTime();
  return val; 
 });

object = _.sortBy(object, 'timestamp');
object.reverse();

console.log(object);