簡體   English   中英

Google Apps 腳本中的升序排序

[英]Ascending sort in Google Apps Script

我有以下格式的列表

[[, 121, , 24603.0, Fri Jul 12 00:00:00 GMT-04:00 2019, sunday, ], 
 [*, 131, , 62531.0, Sat Jul 13 00:00:00 GMT-04:00 2019, monday, ], 
 [*, 141, , 33210.0, Thu Jul 11 00:00:00 GMT-04:00 2019, tuesday, ], 
 [, 142, , -, , , Yes],
 [*, 143, , , Sat Jul 13 00:00:00 GMT-04:00 2019, monday, ]]

我正在嘗試根據日期列(第 5 列)對上述列表進行排序。 結果必須是

[
 [*, 141, , 33210.0, Thu Jul 11 00:00:00 GMT-04:00 2019, tuesday, ], 
 [, 121, , 24603.0, Fri Jul 12 00:00:00 GMT-04:00 2019, sunday, ], 
 [*, 131, , 62531.0, Sat Jul 13 00:00:00 GMT-04:00 2019, monday, ], 
 [*, 143, , , Sat Jul 13 00:00:00 GMT-04:00 2019, monday, ]]
 [, 142, , -, , , Yes]
]

嘗試了以下代碼,但它不起作用:

row.sort({column: 4, ascending: True});

任何線索將不勝感激。

function myFunction() {

  var arr = [
    ["",  121, "", "24603.0", "Fri Jul 12 00:00:00 GMT-04:00 2019", "sunday"  ], 
    ["*", 131, "", "62531.0", "Sat Jul 13 00:00:00 GMT-04:00 2019", "monday"  ], 
    ["*", 141, "", "33210.0", "Thu Jul 11 00:00:00 GMT-04:00 2019", "tuesday" ], 
    ["",  142, "", "-",       "",                                 , "Yes"     ],
    ["*", 143, "", "",        "Sat Jul 13 00:00:00 GMT-04:00 2019", "monday"  ]
  ]

  function compare_dates(date1, date2) {
    if (!date2) return -1;
    const d1 = new Date(date1);
    const d2 = new Date(date2);
    return d1 - d2;
  }

  var sorted_arr = arr.sort((a, b) => compare_dates(a[4], b[4]));

  Logger.log(sorted_arr.join("\n"));
}

output 看起來幾乎如您所願:

*, 141, , 33210.0, Thu Jul 11 00:00:00 GMT-04:00 2019, tuesday
,  121, , 24603.0, Fri Jul 12 00:00:00 GMT-04:00 2019, sunday
*, 143, ,        , Sat Jul 13 00:00:00 GMT-04:00 2019, monday
*, 131, , 62531.0, Sat Jul 13 00:00:00 GMT-04:00 2019, monday
,  142, , -, , , Yes

嘗試這個:

為了將未定義的值強制到底部,我強制它具有一個非常大的數字,以便排序升序將始終導致未定義值位於列表的底部,但通常這可能不是一個好主意。

function sortbyfifth() {
  var arr = [['', 121, '', 24603.0, 'Fri Jul 12 00:00:00 GMT-04:00 2019', 'sunday', ''], ['', 131, '', 62531.0, 'Sat Jul 13 00:00:00 GMT-04:00 2019', 'monday', ''], ['', 141, '', 33210.0, 'Thu Jul 11 00:00:00 GMT-04:00 2019', 'tuesday', ''], ['', 142, '', '-', '', '', 'Yes'], ['', 143, '', '', 'Sat Jul 13 00:00:00 GMT-04:00 2019', 'monday', '']];
  
  arr.sort(function (a, b) {
    var va = 0;
    var vb = 0;
    //var ta = Object.prototype.toString.call(new Date(a[4]));//not required
    //var tb = Object.prototype.toString.call(new Date(b[4]));//not required
    if (Object.prototype.toString.call(new Date(a[4])) === '[object Date]') {
      va = new Date(a[4]).valueOf();
      if (isNaN(va)) {
        va = 10000000000000000;//normally I'd go with zero here but the extremely large number forces undefined to the bottom of sort ascending.  If you reverse the order you'll want to change this to zero. 
      }
    } else {
      va = 10000000000000000;
    }
    if (Object.prototype.toString.call(new Date(b[4])) == '[object Date]') {
      vb = new Date(b[4]).valueOf();
      if (isNaN(vb)) {
        vb = 10000000000000000;
      }
    } else {
      vb = 10000000000000000;
    }
    let tc = Number(va) - Number(vb);
    return va - vb;
  });
  Logger.log(JSON.stringify(arr).replace(/],/g,"],\n"));//I added the replace so that it would be easier to read the output of the console log
}

執行日志

4:11:38 PM  Notice  Execution started
4:11:39 PM  Info    [["",141,"",33210,"Thu Jul 11 00:00:00 GMT-04:00 2019","tuesday",""],
["",121,"",24603,"Fri Jul 12 00:00:00 GMT-04:00 2019","sunday",""],
["",131,"",62531,"Sat Jul 13 00:00:00 GMT-04:00 2019","monday",""],
["",143,"","","Sat Jul 13 00:00:00 GMT-04:00 2019","monday",""],
["",142,"","-","","","Yes"]]
4:11:39 PM  Notice  Execution completed

您可以在邏輯區域多花一點時間來確定 a 和 be 是否都未定義或只是 a 或只是 b。 如果只是 a 則返回 +1 如果只是 b 則返回 -1 如果 a 和 b 都返回 a 0。否則返回 a - b。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM