mirror of
https://github.com/10h30/astroplate.git
synced 2026-07-11 10:46:07 +09:00
26 lines
778 B
TypeScript
26 lines
778 B
TypeScript
// sort by date
|
|
export const sortByDate = (array: any[]) => {
|
|
const sortedArray = array.sort(
|
|
(a:any, b:any) =>
|
|
new Date(b.data.date && b.data.date) -
|
|
new Date(a.data.date && a.data.date)
|
|
);
|
|
return sortedArray;
|
|
};
|
|
|
|
// sort product by weight
|
|
export const sortByWeight = (array: any[]) => {
|
|
const withWeight = array.filter(
|
|
(item: { data: { weight: any } }) => item.data.weight
|
|
);
|
|
const withoutWeight = array.filter(
|
|
(item: { data: { weight: any } }) => !item.data.weight
|
|
);
|
|
const sortedWeightedArray = withWeight.sort(
|
|
(a: { data: { weight: number } }, b: { data: { weight: number } }) =>
|
|
a.data.weight - b.data.weight
|
|
);
|
|
const sortedArray = [...new Set([...sortedWeightedArray, ...withoutWeight])];
|
|
return sortedArray;
|
|
};
|