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

110
Views
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 answers
Answer question

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 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!