I have a list with items in typescript:
let productList = [
{active: false, location: 2, description: "test 123"},
{active: false, location: 15, description: "test 123"},
{active: false, location: 40, description: "test 123"},
{active: false, location: 15, description: "test 123"},
{active: false, location: 1, description: "test 123"},
{active: true, location: 2, description: "test 123"},
{active: false, location: 2, description: "test 123"},
{active: false, location: 7, description: "test 123"},
{active: false, location: 1, description: "test 123"}
]
I want to sort this list so I show on top the the items that has active: true and that have same location for example location: 0
I tried this it sorts by active on top:
productList.sort((a, b) => (b.active - a.active));
Now I want to add location too so those with same location are sorted one after other, one of my tries was this but no success:
productList.sort((a, b) => (b.active - a.active)&& ((a.location === b.location) ? 1 : -1) );
My end result I want to have is to show active on top and show immediately after those with same location as active
let productList = [
{active: true, location: 2, description: "test 123"},
{active: false, location: 2, description: "test 123"},
{active: false, location: 2, description: "test 123"},
{active: false, location: 1, description: "test 123"},
{active: false, location: 1, description: "test 123"},
{active: false, location: 7, description: "test 123"},
{active: false, location: 15, description: "test 123"},
{active: false, location: 15, description: "test 123"},
{active: false, location: 40, description: "test 123"},
]
Can someone help me please? I'm stuck. I tried several ways but not success.
You'll need to change your first condition to check if any of the elements that match the current a and b locations are active, not just the currently iterated elements. Here using some().
let productList = [
{ active: false, location: 2, description: "test 123" },
{ active: false, location: 15, description: "test 123" },
{ active: false, location: 40, description: "test 123" },
{ active: false, location: 15, description: "test 123" },
{ active: false, location: 1, description: "test 123" },
{ active: true, location: 2, description: "test 123" },
{ active: false, location: 2, description: "test 123" },
{ active: false, location: 7, description: "test 123" },
{ active: false, location: 1, description: "test 123" }
];
const hasActive = (o) => Number(productList.some(e => o.location === e.location && e.active));
const compare = (a, b) => (
hasActive(b) - hasActive(a)
|| a.location - b.location
|| b.active - a.active
);
console.log(productList.sort(compare));
A more involved solution which first groups by location, then sorts and maps back to an ungrouped array.
function groupSort(arr) {
const grouped = {};
for (const object of arr) {
const { location, active } = object;
grouped[location] ??= { location, active, objects: [] };
grouped[location].objects.push(object);
if (active) {
grouped[location].active = true;
}
}
return Object.values(grouped)
.sort((a, b) => b.active - a.active || a.location - b.location)
.flatMap(({ objects }) => objects.sort((a, b) => b.active - a.active));
};
const productList = [{ active: false, location: 2, description: "test 123" }, { active: false, location: 15, description: "test 123" }, { active: false, location: 40, description: "test 123" }, { active: false, location: 15, description: "test 123" }, { active: false, location: 1, description: "test 123" }, { active: true, location: 2, description: "test 123" }, { active: false, location: 2, description: "test 123" }, { active: false, location: 7, description: "test 123" }, { active: true, location: 1, description: "test 123" }];
const sorted = groupSort(productList);
console.log(sorted);