如何对下面的对象数组进行排序(按日期DESC)

时间:2017-01-31 11:39:20

标签: javascript

假设我有一系列对象。我想按日期排序,降序。我怎么能用javascript做到这一点?这是我试图排序的对象

[
    {
        "577": {
            "id": "577",
            "status": "",
            "date": "09 January 2017",
            
        },
        "594": {
            "id": "594",
            "status": "",
            "date": "01 January 2017",
            
        },
        "595": {
            "id": "595",
            "status": "",
            "date": "02 January 2017",
            
        },
        "596": {
            "id": "596",
            "status": "",
            "date": "03 January 2017",
           
        }
        
    }
]

1 个答案:

答案 0 :(得分:0)

您不对对象进行排序,您对数组进行排序:

var list = [
  {
    "id": "577",
    "status": "",
    "date": "09 January 2017"
  }, {
    "id": "594",
    "status": "",
    "date": "01 January 2017"
  }, {
    "id": "595",
    "status": "",
    "date": "02 January 2017"
  }, {
    "id": "596",
    "status": "",
    "date": "03 January 2017"
  }
].sort(function(a, b) {
  return new Date(b.date).getTime() > new Date(a.date).getTime()
});

console.log(list)