帮我写一个 xxxx-xx-xx 时间排序方法 js
function sortByDate(arr) { arr.sort(function(a, b) { var dateA = new Date(a); var dateB = new Date(b); return dateA - dateB; }); return arr; }
// 调用示例 var dateArray = ['2022-01-01', '2021-12-31', '2022-02-01']; console.log(sortByDate(dateArray)); // 输出 ["2021-12-31", "2022-01-01", "2022-02-01"]
原文地址: https://www.cveoy.top/t/topic/hBdZ 著作权归作者所有。请勿转载和采集!