按日期对JSON数据排序Google应用程序脚本

按日期对JSON数据排序Google应用程序脚本,json,google-apps-script,Json,Google Apps Script,我正在运行一个基本API调用,该调用使用以下Google Apps脚本: var response = UrlFetchApp.fetch('https://url.com?startDate=2017-09-01&endDate=2018-04-01', options); var dataAll = JSON.parse(response.getContentText()); 这为我提供了以下JSON数据 { "kind": "resultTabl

我正在运行一个基本API调用,该调用使用以下Google Apps脚本:

var response = UrlFetchApp.fetch('https://url.com?startDate=2017-09-01&endDate=2018-04-01', options);
var dataAll = JSON.parse(response.getContentText());
这为我提供了以下JSON数据

{
  "kind": "resultTable",
  "columnHeaders": [
    {
      "name": "month",
      "columnType": "DIMENSION",
      "dataType": "STRING"
    },
    {
      "name": "averageViewDuration",
      "columnType": "METRIC",
      "dataType": "INTEGER"
    },
    {
      "name": "averageViewPercentage",
      "columnType": "METRIC",
      "dataType": "FLOAT"
    },
    {
      "name": "subscribersGained",
      "columnType": "METRIC",
      "dataType": "INTEGER"
    },
    {
      "name": "estimatedMinutesWatched",
      "columnType": "METRIC",
      "dataType": "INTEGER"
    },
    {
      "name": "views",
      "columnType": "METRIC",
      "dataType": "INTEGER"
    },
    {
      "name": "likes",
      "columnType": "METRIC",
      "dataType": "INTEGER"
    },
    {
      "name": "subscribersGained",
      "columnType": "METRIC",
      "dataType": "INTEGER"
    },
    {
      "name": "shares",
      "columnType": "METRIC",
      "dataType": "INTEGER"
    }
  ],
  "rows": [
    [
      "2017-11",
      648,
      22.06,
      13,
      47298,
      4376,
      60,
      13,
      72
    ],
    [
      "2017-12",
      641,
      21.83,
      6,
      21284,
      1990,
      13,
      6,
      14
    ],
    [
      "2018-01",
      620,
      21.12,
      5,
      12790,
      1236,
      14,
      5,
      12
    ],
    [
      "2018-02",
      636,
      21.65,
      2,
      11374,
      1072,
      5,
      2,
      12
    ],
    [
      "2018-04",
      604,
      20.58,
      4,
      9760,
      968,
      14,
      4,
      9
    ],
    [
      "2018-03",
      615,
      20.94,
      8,
      8486,
      827,
      7,
      8,
      12
    ]
  ]
}   
虽然行大多按日期顺序排列,但也有一两个实例不按日期顺序排列。我如何在Google Apps脚本中按日期对行进行排序,使其从2017-11年开始,最后从2018-04年开始?

类似这样的内容

function sortRows() {
  var response = UrlFetchApp.fetch(); 
  let data=JSON.parse(response.getContentText());//from what you provided
  data.rows.sort((a,b)=>{
    let A=a[0].split('-');
    let adv=new Date(A[0],A[1],0).valueOf();
    let B=b[0].split('-');
    let bdv=new Date(B[0],B[1],0).valueOf();
    return adv-bdv;
  });
}

这些行似乎是一个二维数组。可以将sort方法与sort函数结合使用,该函数使用完整的year和month-1以及Date()构造函数创建一个新的Date(),然后通过valueOf或getTime方法比较这些值。这非常有效。非常感谢你!