Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
Sort list by two fields doesn't work typescript

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!