最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sorting by date with underscore.js or just plain JS - Stack Overflow

programmeradmin0浏览0评论

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!

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!

Share Improve this question asked Apr 19, 2012 at 23:09 29er29er 9,00512 gold badges49 silver badges65 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 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/

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.

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.

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);

发布评论

评论列表(0)

  1. 暂无评论