Array con ordenación de objetos con Subrayado sortBy


Tengo esta matriz. Cómo uso el subrayado'_.Sortby ' para ordenar de acuerdo a la fecha de inicio?

[
    { 
        id: 'oljw832021kjnb389xzll323jk',
        start: { dateTime: '2013-09-26T13:30:00-07:00' },
        end: { dateTime: '2013-09-26T14:30:00-07:00' },
    },
    { 
        id: 'ed7l5tmckdp0lm90nvr4is3d4c',
        start: { dateTime: '2013-09-26T15:30:00-07:00' },
        end: { dateTime: '2013-09-26T16:30:00-07:00' },
    },
    { 
        id: 'etmasdsackdp0kjl0nvrkopioqw',
        start: { dateTime: '2013-09-26T18:00:00-07:00' },
        end: { dateTime: '2013-09-26T19:00:00-07:00' },
    }
]
Author: Thaddeus Albers, 2013-09-30

2 answers

Utilice una función iteradora, no una sola cadena para una propiedad:

_.sortBy(arr, function(o) { return o.start.dateTime; })
 150
Author: Bergi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-30 09:24:48

Lo hice de esta manera:

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

Si lo quieres descendiendo entonces es lo mismo pero *-1

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

En este ejemplo estoy ordenando por dos campos, puede olvidarse del artículo.BatchID.

Espero que esto ayude a alguien.

 1
Author: Sanchitos,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-24 17:53:32