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

112
Visualizações
How does one sort an array of objects by an object's property value which itself is not naturally comparable but has a rule based precedence?

I have an object. I need to sort it by TranType Property.
The Trantype are like CS+,CS-,RS+,RS-, OPO, OPI, Security in, Security out. I need to sort it by CS+,CS-,RS+,RS-, OPO, OPI, Security in, Security out

Example:

[
  {
    'TranType':'CS+',
    'Name': 'Kumar'
  },
  {
    'TranType':'RS+',
    'Name': 'Lak'
  },
  {
    'TranType':'CS+',
    'Name': 'Cnk'
  },
  {
    'TranType':'CS-',
    'Name': 'Pro'
  },
  {
    'TranType':'CS-',
    'Name': 'Lhj'
  },
  {
    'TranType':'RS-',
    'Name': 'Speed'
  },
  {
    'TranType':'OPI',
    'Name': 'Neck'
  },
  {
    'TranType':'OPO',
    'Name': 'Dok'
  },
  {
    'TranType':'Security In',
    'Name': 'Kol'
  },
  {
    'TranType':'Security out',
    'Name': 'Klp'
  },
  {
    'TranType':'OPI',
    'Name': 'Tpi'
  },
  {
    'TranType':'RS+',
    'Name': 'Alo'
  },
  {
    'TranType':'OPO',
    'Name': 'Tpp'
  },
  {
    'TranType':'Security In',
    'Name': 'Jkl'
  },
  {
    'TranType':'RS-',
    'Name': 'Aoi'
  },
  {
    'TranType':'Security out',
    'Name': 'Nko'
  }
]

I need to sort it as below one.

Here I need to have the trantyoe of ( CS+, OPI, RS+, Security In ) are first priority. And the ( CS-, RS-, OPO, Security out) are second priority.

[
  {
    'TranType':'CS+',
    'Name': 'Kumar'
  },
  {
    'TranType':'CS+',
    'Name': 'Cnk'
  },
  {
    'TranType':'OPI',
    'Name': 'Neck'
  },
  {
    'TranType':'OPI',
    'Name': 'Tpi'
  },
  {
    'TranType':'RS+',
    'Name': 'Lak'
  },
  {
    'TranType':'RS+',
    'Name': 'Alo'
  },
  {
    'TranType':'Security In',
    'Name': 'Kol'
  },
  {
    'TranType':'Security In',
    'Name': 'Jkl'
  },
  {
    'TranType':'CS-',
    'Name': 'Pro'
  },
  {
    'TranType':'CS-',
    'Name': 'Lhj'
  },
  {
    'TranType':'RS-',
    'Name': 'Speed'
  },
  {
    'TranType':'RS-',
    'Name': 'Aoi'
  },
  {
    'TranType':'OPO',
    'Name': 'Dok'
  },
  {
    'TranType':'OPO',
    'Name': 'Tpp'
  },
  {
    'TranType':'Security out',
    'Name': 'Klp'
  },
  {
    'TranType':'Security out',
    'Name': 'Nko'
  }
]

Please help me to get the output as expected.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The easiest and most straightforward approach is to make use of an object as lookup for TranType precedence values ...

const tranTypePrecedences = {
  'CS+': 1,
  'OPI': 2,
  'RS+': 3,
  'Security In': 4,
  'CS-': 5,
  'RS-': 6,
  'OPO': 7,
  'Security out': 8,
};

console.log(
  [{

    'TranType':'CS+',
    'Name': 'Kumar'
  }, {
    'TranType':'RS+',
    'Name': 'Lak'
  }, {
    'TranType':'CS+',
    'Name': 'Cnk'
  }, {
    'TranType':'CS-',
    'Name': 'Pro'
  }, {
    'TranType':'CS-',
    'Name': 'Lhj'
  }, {
    'TranType':'RS-',
    'Name': 'Speed'
  }, {
    'TranType':'OPI',
    'Name': 'Neck'
  }, {
    'TranType':'OPO',
    'Name': 'Dok'
  }, {
    'TranType':'Security In',
    'Name': 'Kol'
  }, {
    'TranType':'Security out',
    'Name': 'Klp'
  }, {
    'TranType':'OPI',
    'Name': 'Tpi'
  }, {
    'TranType':'RS+',
    'Name': 'Alo'
  }, {
    'TranType':'OPO',
    'Name': 'Tpp'
  }, {
    'TranType':'Security In',
    'Name': 'Jkl'
  }, {
    'TranType':'RS-',
    'Name': 'Aoi'
  }, {
    'TranType':'Security out',
    'Name': 'Nko'

  }].sort((a, b) =>
    tranTypePrecedences[a.TranType] - tranTypePrecedences[b.TranType]
  )
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

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