gpt4 book ai didi

javascript 按最近的开始日期时间排序以显示最远的距离和另一个属性

转载 作者:数据小太阳 更新时间:2023-10-29 05:37:12 26 4
gpt4 key购买 nike

我必须按开始日期时间进行排序,并引用具有多个属性的当前日期时间。例如,我有这个列表:

<ul>
<li data-startDate="09-04-2019 15:00" data-inplay=false>Name1</li>
<li data-startDate="09-04-2019 15:30" data-inplay=false>Name2</li>
<li data-startDate="09-04-2019 15:20" data-inplay=false>Name3</li>
<li data-startDate="09-04-2019 16:00" data-inplay=false>Name4</li>
<li data-startDate="09-04-2019 15:00" data-inplay=true> Name5</li>
<li data-startDate="09-04-2019 15:10" data-inplay=false>Name6</li>
<li data-startDate="09-04-2019 15:05" data-inplay=true> Name7</li>
<li data-startDate="08-04-2019 12:05" data-inplay=false>Name8</li>
</ul>

并且当前日期时间是 09-04-2019 15:15,所需的输出应该是:

<!--
Let currentDate = 09-04-2019 15:15
Sort priority:
1. inplay is true
2. startDate is greater than currentDate
3. startDate is lesser than currentDate
-->
<ul>
<!-- inplay == true -->
<li data-startDate="09-04-2019 15:05" data-inplay=true>Name7</li>
<li data-startDate="09-04-2019 15:00" data-inplay=true>Name5</li>

<!-- startDate > currentDate -->
<li data-startDate="09-04-2019 15:20" data-inplay=false>Name3</li>
<li data-startDate="09-04-2019 15:30" data-inplay=false>Name2</li>
<li data-startDate="09-04-2019 16:00" data-inplay=false>Name4</li>

<!-- startDate < currentDate -->
<li data-startDate="09-04-2019 15:10" data-inplay=false>Name6</li>
<li data-startDate="09-04-2019 15:00" data-inplay=false>Name1</li>
<li data-startDate="08-04-2019 12:05" data-inplay=false>Name8</li>
</ul>

我试过了,但不包括 inplay 参数,而且顺序也不完全正确

var list = $("ul > li");
var target = new Date().getTime();
var sortByStartTime = function (a, b) {
var ret = 0;
var startDateA = $(a).data("startdate");
var startDateB = $(b).data("startdate");
var dateA = startDateA.split(" ")[0].split("-");
var timeA = startDateA.split(" ")[1].split(":");
var dateB = startDateB.split(" ")[0].split("-");
var timeB = startDateB.split(" ")[1].split(":");
startDateA = new Date(dateA[2], dateA[1] - 1, dateA[0], timeA[0], timeA[1]);
startDateB = new Date(dateB[2], dateB[1] - 1, dateB[0], timeB[0], timeB[1]);
var dA = Math.abs(startDateA.getTime() - target);
var dB = Math.abs(startDateB.getTime() - target);
if (dA < dB) {
ret = -1;
} else if (dA > dB) {
ret = 1;
} else {
ret = 0;
}
return ret;
};

list.sort(sortByStartTime);

for (var i = 0; i < list.length; i = i + 1) {
list[i].parentNode.appendChild(list[i]);
}

基本上,列表应该按最近的开始日期时间(相对于当前日期时间)到最远的顺序排列,但如果有一些内容,它们应该位于列表的顶部,按最近的开始日期时间(相对于当前日期时间)排列时间)到最远。我希望我解释得足够清楚。感谢您提供任何帮助,谢谢。

最佳答案

排序逻辑:

  1. 根据数据输入真假排序,使用数字简化排序逻辑(降序排序)
  2. 如果差异相反零的一侧先保持正值,后保持负值(降序排列)
  3. 如果与当前时间不同因为两个日期都在 0 的同一侧,按它们的绝对值排序值(升序排列)

var list = Array.from(document.querySelectorAll("ul > li"));
var target = new Date('09-04-2019 15:15');
var boolValue = { true: 1, false: 0 };
var sortByStartTime = function (a, b) {
var diffA = new Date(a.dataset.startdate) - target;
var diffB = new Date(b.dataset.startdate) - target;
var inplayA = boolValue[a.dataset.inplay];
var inplayB = boolValue[b.dataset.inplay];
return inplayB - inplayA || (
diffA < 0 === diffB < 0 ? Math.abs(diffA) - Math.abs(diffB) : diffB - diffA
);
};

list.sort(sortByStartTime);

for (var i = 0; i < list.length; i = i + 1) {
list[i].parentNode.appendChild(list[i]);
}
<ul>
<li data-startDate="09-04-2019 15:00" data-inplay=false>Name1</li>
<li data-startDate="09-04-2019 15:30" data-inplay=false>Name2</li>
<li data-startDate="09-04-2019 15:20" data-inplay=false>Name3</li>
<li data-startDate="09-04-2019 16:00" data-inplay=false>Name4</li>
<li data-startDate="09-04-2019 15:00" data-inplay=true> Name5</li>
<li data-startDate="09-04-2019 15:10" data-inplay=false>Name6</li>
<li data-startDate="09-04-2019 15:05" data-inplay=true> Name7</li>
<li data-startDate="08-04-2019 12:05" data-inplay=false>Name8</li>
</ul>

PS: replace new Date('09-04-2019 15:15'); with new Date(); to get what you wanted. I hardcoded the date to show that results are as per your requirement.

关于javascript 按最近的开始日期时间排序以显示最远的距离和另一个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55587709/

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