Home > database >  How does one sort an array of objects by an object's property value which itself is not natural
How does one sort an array of objects by an object's property value which itself is not natural

Time:10-21

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.

CodePudding user response:

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; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related