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

211
Views
Sorting by 3 different criteria in JavaScript

I am trying to sort this List below, first by Relationship(If it is subscriber then it should be first) then by Active vs inactive. Active should be before Inactive then each of the active and inactive should be sorted by descending age order. So I have this array:

[{status: 'Active', BirthDate: '12/31/1960', Relationship:"Friend"},
{status: 'Active', BirthDate: '12/31/1985', Relationship:"Wife"},
{status: 'Inactive', BirthDate: '12/31/1998', Relationship:"Son"},
{status: 'Inactive', BirthDate: '12/31/1996',Relationship:"Daughter"},
{status: 'Active', BirthDate: '12/31/1995', Relationship:"Daughter"},
{status: 'Active', BirthDate: '12/31/1979', Relationship:"Subscriber"},
{status: 'Inactive', BirthDate: '12/31/1999', Relationship:"Son"}
]

and the result should be:

[{status: 'Active', BirthDate: '12/31/1979', Relationship:"Subscriber"},
{status: 'Active', BirthDate: '12/31/1960', Relationship:"Friend"},
{status: 'Active', BirthDate: '12/31/1985', Relationship:"Wife"},
{status: 'Active', BirthDate: '12/31/1995', Relationship:"Daughter"},
{status: 'Inactive', BirthDate: '12/31/1992', Relationship:"Son"},
{status: 'Inactive', BirthDate: '12/31/1996',Relationship:"Daughter"},
{status: 'Inactive', BirthDate: '12/31/1999', Relationship:"Son"}
]

I tried this below but doesnt seem to work:

testlist = testlist.sort((a, b) => (a.Relationship === "Subscriber" ) ? -1 :(a.status === "Active") ? -2 : (b.BirthDate - a.BirthDate) ? -3 : 1);

Is there a way to do this by using the .sort method in JavaScript?

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

0

Using Array#sort:

const arr = [ {status: 'Active', BirthDate: '12/31/1960', Relationship:"Friend"}, {status: 'Active', BirthDate: '12/31/1985', Relationship:"Wife"}, {status: 'Inactive', BirthDate: '12/31/1998', Relationship:"Son"}, {status: 'Inactive', BirthDate: '12/31/1996',Relationship:"Daughter"}, {status: 'Active', BirthDate: '12/31/1995', Relationship:"Daughter"}, {status: 'Active', BirthDate: '12/31/1979', Relationship:"Subscriber"}, {status: 'Inactive', BirthDate: '12/31/1999', Relationship:"Son"} ];

const sorted = arr.sort(
  (
    { status: statusA, Relationship: relationshipA, BirthDate: birthDateA }, 
    { status: statusB, Relationship: relationshipB, BirthDate: birthDateB }
  ) => {
    const relationship = (relationshipB === 'Subscriber') - (relationshipA === 'Subscriber');
    if(relationship) return relationship;
    const status = (statusB === 'Active') - (statusA === 'Active');
    if(status) return status;
    const birthday = (new Date(birthDateA)).getTime() - (new Date(birthDateB)).getTime();
    return birthday;
  });

console.log(sorted);

about 4 years ago · Juan Pablo Isaza Report

0

Here's one way to do it

let arr = [{status: 'Active', BirthDate: '12/31/1960', Relationship:"Friend"},
{status: 'Active', BirthDate: '12/31/1985', Relationship:"Wife"},
{status: 'Inactive', BirthDate: '12/31/1998', Relationship:"Son"},
{status: 'Inactive', BirthDate: '12/31/1996',Relationship:"Daughter"},
{status: 'Active', BirthDate: '12/31/1995', Relationship:"Daughter"},
{status: 'Active', BirthDate: '12/31/1979', Relationship:"Subscriber"},
{status: 'Inactive', BirthDate: '12/31/1999', Relationship:"Son"}
]

arr.sort((a,b)=> (a.status.localeCompare(b.status) || new Date(a.BirthDate) - new Date(b.BirthDate)));


let index = arr.findIndex(i => i.Relationship === "Subscriber")
arr.unshift(arr.splice(index, 1)[0])

console.log(arr)

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!