我有物件陣列:
const data = [{
"id": "1",
"effectiveDate": "2023-01-21"
}, {
"id": "2",
"effectiveDate": "2023-02-22"
}, {
"id": "3",
"effectiveDate": "2022-05-04"
}, {
"id": "4",
"effectiveDate": "2022-05-05"
}, {
"id": "5",
"effectiveDate": "2021-01-21"
}, {
"id": "6",
"effectiveDate": "2021-02-22"
}];
我所追求的是以具有最新日期的物件位于索引 0 上的方式對其進行排序的方式,其余物件從最舊的日期到未來的日期按升序排列,如下所示:
[{
"id": "4",
"effectiveDate": "2022-05-05"
}, {
"id": "5",
"effectiveDate": "2021-01-21"
}, {
"id": "6",
"effectiveDate": "2021-02-22"
}, {
"id": "3",
"effectiveDate": "2022-05-04"
}, {
"id": "1",
"effectiveDate": "2023-01-21"
}, {
"id": "2",
"effectiveDate": "2023-02-22"
}]
我排序的方式是:
const orderedDates = data.sort((a, b) => {
const dateCompareResult = new Date(a.effectiveDate) - new Date(b.effectiveDate);
return dateCompareResult;
});
這顯然給了我從過去日期到未來日期升序排列的日期,最新日期介于兩者之間。如何將最新的日期物件移動到索引 0?
uj5u.com熱心網友回復:
function getItemsInAscendingDateOrderAndClosestToNowFirst(arr) {
const time = Date.now();
const [closest, ...rest] = Array
// create a shallow copy in order to
// not mutate the original reference.
.from(arr)
// sort items by date closest to now.
.sort((a, b) => {
const aTime = new Date(a.effectiveDate).getTime();
const bTime = new Date(b.effectiveDate).getTime();
const aDelta = Math.abs(time - aTime);
const bDelta = Math.abs(time - bTime);
return (aDelta - bDelta);
});
return [
closest,
...rest
// sort all other items in ascending date order.
.sort((a, b) =>
new Date(a.effectiveDate).getTime()
- new Date(b.effectiveDate).getTime()
),
];
}
const data = [{
"id": "1",
"effectiveDate": "2023-01-21"
}, {
"id": "2",
"effectiveDate": "2023-02-22"
}, {
"id": "3",
"effectiveDate": "2022-05-04"
}, {
"id": "4",
"effectiveDate": "2022-05-05"
}, {
"id": "5",
"effectiveDate": "2021-01-21"
}, {
"id": "6",
"effectiveDate": "2021-02-22"
}];
const sortedData =
getItemsInAscendingDateOrderAndClosestToNowFirst(data);
console.log({ sortedData });
.as-console-wrapper { min-height: 100%!important; top: 0; }
uj5u.com熱心網友回復:
我認為最簡單的方法是從最舊到最新排序,然后將最接近今天的元素移到開頭,例如
let data = [{"id": "1","effectiveDate": "2023-01-21"},
{"id": "2","effectiveDate": "2023-02-22"},
{"id": "3","effectiveDate": "2022-05-04"},
{"id": "4","effectiveDate": "2022-05-05"},
{"id": "5","effectiveDate": "2021-01-21"},
{"id": "6","effectiveDate": "2021-02-22"}];
// Sort from oldest to youngest
data.sort((a, b) => b.effectiveDate.localeCompare(a.effectiveDate));
// Get today as milliseconds
let today = new Date().toLocaleDateString('en-CA');
let todayMS = Date.parse(today);
// Get index of closest element to today
let closestIndex = data.reduce((acc, obj, index) => {
let diff = Math.abs(Date.parse(obj.effectiveDate) - todayMS);
return acc.diff > diff? {diff, index} : acc;
}, {diff: Infinity, index:null}).index;
// Move closest element to start of array
data.unshift(data.splice(closestIndex, 1));
console.log(data);
ISO 8601 格式時間戳的好處之一是詞法比較給出了正確的結果。如果您不想使用字串比較,那么比較函式可以是:
data.sort((a, b) => Date.parse(b) - Date.parse(a))
但前提是時間戳是 ECMA-262 支持的 3 種格式之一(幸運的是,YYYY-MM-DD 是其中一種格式)。:-)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/474647.html
標籤:javascript 数组 日期 排序 目的
上一篇:對陣列進行排序,使零值位于每組的底部,其余值按字母順序排序
下一篇:按列中的列值對多維陣列進行排序
