generated at
JSONを日付順にソート
ref.
JSONのデータに、日付情報が含まれている場合に、それを使って項目をソートする

以下はGeminiに聞いたもの
sample.json
[ { "id": "603c48eaef0d7015a1d9cc9cf2836354", "title": "『魔女に首輪は付けられない (電撃文庫)』", "created": 1708322233581, "updated": 1708322233581, "accessed": 1708322233581, "lines": [], "descriptions": "", "image": "image/books/4049155257.jpg", "pin": 0, "address": [], "type": "media", "media-info": { "media": "book", "author": { "著": [ "夢見夕利" ], "イラスト": [ "緜" ] }, "publisher": "KADOKAWA", "pubdate": "2024-02-09", "ASIN": "4049155257" }, "state": { "buy": "2024-02-15", "touch": "2024-02-19", "start": null, "end": "2024-02-15" } } ]

sample.js
// JSONデータを解析 const parsedData = JSON.parse(jsonData); // "end"の日付でソート parsedData.sort((a, b) => { const dateA = new Date(a.state.end); const dateB = new Date(b.state.end); return dateA - dateB; //return dateB - dateA;とすれば昇降がひっくりかえる }); // ソート後のデータを出力 console.log(parsedData);

"end": null のような項目があるとそこでソートが途切れる場合

script.js
// JSONデータを解析 const parsedData = JSON.parse(jsonData); // "end"の日付でソート parsedData.sort((a, b) => { // "end"の値がnullの場合は、比較対象外の値として扱います。 if (a.state.end === null) { return 1; } else if (b.state.end === null) { return -1; } const dateA = new Date(a.state.end); const dateB = new Date(b.state.end); return dateA - dateB; }); // ソート後のデータを出力 console.log(parsedData);