Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda