javascript - 如何对 luxon 对象数组执行排序
问题描述
我正在尝试使用 js 类型的 luxon 时间对象。我不确定这是否如 sort 所期望的那样正确-1,0,1
const results = objectGroupedByYearMonth[year][month].results.sort(
(a,b) =>
DateTime.fromISO(b.created)
.diff(DateTime.fromISO(a.created))
)
这将返回一个 dt 对象console.log("DIF: ", DateTime.fromISO("2020-11-03T17:01:22.205041Z") .diff(DateTime.fromISO("2020-11-03T17:15:23.998284Z")))
解决方案
您可能希望将对象转换为DateTime
数字 Unix 时间戳:
function compareLuxonDates(a: DateTime, b: DateTime) {
return a.toMillis() - b.toMillis()
}
arrayWithDateObjects.sort(compareLuxonDates)
推荐阅读
- openedge - 带有 Web 服务的 PASOE 业务类实体中带有令牌的自定义标头?
- html - 简单的电子邮件 CSS 显示与浏览器中的不同
- python - Python CGI 脚本根本不渲染
- ios - 在 iOS 10 上的 WKWebView 中打开 PDF 文件时启用捏合缩放手势
- gitlab - 我笔记本电脑上的 Gitlab CI 运行程序无法执行 ci,因为它无权访问 CI_XXX 环境变量
- javascript - jQuery:如何获取选定单元格的列和行标题
- python - 使用贝塞尔曲线的平滑 2d 布朗步走
- azure - 创建资源组并部署资源
- laravel - Laravel 计数 > 其他计数
- reactjs - 如何解决我的 react-dropzone 应用程序的“道具类型错误”