Javascript 排序对象日期

Javascript sorting Object dates

循环后得到以下数组。是否可以将其从当前日期排序到最新日期。

//Sortering
    var arr = {};
    var key = "";
    var teller = 0;
    for (var i = 0; i < schedule_id.length; i++) {

    //Ajax call maken
    $.ajax({
        url: "http://api.viewer.zmags.com/schedules/" + schedule_id[i] + "?key=" + api_key
    })
    //WdInit after 10 calls
    .done(function(data){

        //Check publicatieID is not null
        if (undefined === data.scheduleEntries[default_pub]|| null === data.scheduleEntries[default_pub]) {
        }
        else
        {       

            var key = schedule_id[teller];
            //loopen doorheen resultaat call
            $.each(data.scheduleEntries, function(index, entry){
                arr[key] = entry.startDate;
            })
        }   
        teller++;
    })
    }

arr: Object
7aaabbec: "2015-02-09T23:00:00.000Z"
31ba19e7: "2015-02-24T23:01:00.000Z"
31ff78e7: "2015-02-24T23:01:00.000Z"
159a11a7: "2015-02-10T23:01:00.000Z"
1339d0e9: "2015-02-17T23:01:00.000Z"

我已经得到代码但错误:未定义不是函数

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

对象没有顺序。你不能订购一个对象。你必须先把它变成一个数组,它又不能有任意键,但可以排序。

在Javascript中:

  • 对象:未定义顺序的键值集合
  • 数组:有顺序的列表,但没有(有意义的)键

使用普通数组存储值,以便您可以对它们进行排序:

arr = [];
loopen doorheen resultaat call
$.each(data.scheduleEntries, function(index, entry){
     arr.push( {'key':key, 'date':entry.startDate} );
})

arr.sort(function(a, b){
    return a['date'] > b['date']; //this may need a better comparision...
});

更新: 排序后提取散列键只需循环数组:

var sorted_keys = [];
for(var i = 0; i < arr.length; i++){
  sorted_keys.push( arr[i] );
}

首先从对象中获取键,为此你使用类似underscore.js的东西:

var keyList = _.keys( arr );

// sort keyList
keyList = keyList.sort();

// Now, do anything by getting values for sorted keys
for ( key in keyList ) {
    console.log( arr[ key ] );
}

请阅读您的最新评论...对于您的情况,您可以先转换为列表列表或键-值对列表

// convert into List of [ key, value ]
val pairList = _.pairs( arr );

// sort using values
pairList = pairList.sort( function( a, b ) {
    // use moment.js to get date functions
    var aMoment = moment( a[ 1 ] )
    var bMoment = moment( b[ 1 ] )

    // change based on reverse-sorted or sorted.
    return bMement.diff( aMement );
} );

// Now sorted use as you want
for ( keyVal in pairList ) {
    console.log( keyVal[ 0 ] + " -> " + keyVal[ 0 ] );
}

通过循环对象,将所有日期放入数组中并对其进行排序,然后再次生成对象

            var myObj = {
                '7aaabbec': "2015-02-09T23:00:00.000Z",
                '31ba19e7': "2015-02-24T23:01:00.000Z",
                '31ff78e7': "2015-02-24T23:01:00.000Z",
                '159a11a7': "2015-02-10T23:01:00.000Z",
                '1339d0e9': "2015-02-17T23:01:00.000Z"
            };
            var timeArray = [], newObj = {};
            for(var key in myObj){
              timeArray.push([key,myObj[key]]); 
            }
            timeArray.sort(function(a, b) {return new Date(a[1]) - new Date(b[1])});
            //console.log(timeArray);
            var j=0,k=1;
            for(var i=0;i<timeArray.length;i++){
              newObj[timeArray[i][j]] = new Date(timeArray[i][k]);
            }
            $("#result").html(JSON.stringify(newObj));

工作示例是here