gpt4 book ai didi

javascript - 按字符串日期值对数组列表进行排序

转载 作者:行者123 更新时间:2023-12-04 09:29:27 25 4
gpt4 key购买 nike

data = [["Apples", 606.8, "pos151.5", "12-11-19"],
["Apples", 608.8, "pos213.9", "12-11-19"],
["Apples", 610.5, "pos271.6", "12-11-19"],
["Apples", 612.5, "pos325", "12-11-19"],
["Apples", 609.4, "pos521.7", "12-11-19"],
["Apples", 611.1, "pos622.8", "12-11-19"],
["Bananas", 626.1, "pos175", "02-20-20"],
["Bananas", 626.1, "pos234.2", "02-20-20"],
["Bananas", 630, "pos321", "02-20-20"],
["Bananas", 620.6, "pos97", "02-20-20"],
["Pears", 616.5, "pos150.9", "10-17-19"],
["Pears", 616.6, "pos17.2", "10-17-19"],
["Pears", 615.4, "pos300.9", "10-17-19"],
["Pears", 614, "pos446.9", "10-17-19"],
["Oranges", 616.1, "pos127.69", "10-21-19"],
["Oranges", 616.1, "pos177.49", "10-21-19"],
["Oranges", 616.1, "pos233.69", "10-21-19"],
["Oranges", 616.5, "pos383.73", "10-21-19"],
["Oranges", 616.3, "pos546.93", "10-21-19"],
["Oranges", 615.2, "pos637.99", "10-21-19"]]
我将如何按每个数组(日期字符串)中的第 4 个值按升序对这个数组列表进行排序?
我假设它是这样的,但我不确定如何在没有字段名称/键的情况下调用数组中的第 4 个值:
data.sort(function(a,b) {
return new Date(a.date).getTime() - new Date(b.date).getTime()
});
谢谢。

最佳答案

当没有为数组设置键时,只需通过基于零的数字索引即可访问它们。因此,要访问第 4 项,您将使用 data[3] .因此,鉴于您的排序示例,

data.sort(function(a,b) {
return new Date(a[3]).getTime() - new Date(b[3]).getTime()
});

关于javascript - 按字符串日期值对数组列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62905201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com