mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-10-27 16:02:26 +00:00
33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
// js/feedEngine/sorters.js
|
|
|
|
export function createChronologicalSorter({ direction = "desc" } = {}) {
|
|
const normalizedDirection = direction === "asc" ? "asc" : "desc";
|
|
|
|
return function chronologicalSorter(items = []) {
|
|
if (!Array.isArray(items)) {
|
|
return [];
|
|
}
|
|
|
|
const copy = [...items];
|
|
copy.sort((a, b) => {
|
|
const aTs = Number(a?.video?.created_at);
|
|
const bTs = Number(b?.video?.created_at);
|
|
const normalizedATs = Number.isFinite(aTs) ? aTs : Number.NEGATIVE_INFINITY;
|
|
const normalizedBTs = Number.isFinite(bTs) ? bTs : Number.NEGATIVE_INFINITY;
|
|
|
|
if (normalizedATs !== normalizedBTs) {
|
|
const diff = normalizedATs - normalizedBTs;
|
|
return normalizedDirection === "asc" ? diff : -diff;
|
|
}
|
|
|
|
const aId = typeof a?.video?.id === "string" ? a.video.id : "";
|
|
const bId = typeof b?.video?.id === "string" ? b.video.id : "";
|
|
return normalizedDirection === "asc"
|
|
? aId.localeCompare(bId)
|
|
: bId.localeCompare(aId);
|
|
});
|
|
|
|
return copy;
|
|
};
|
|
}
|