Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda