How to sort it in Javascript

I have an object. I need to sort it by TranType Property. The Trantype are … Read more How to sort it in Javascript

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.

Source: JavaSript – Stack Overflow


Categories: NewsTags:

Tags:


Leave a Reply

Your email address will not be published. Required fields are marked *