-3

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.

Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
  • 2
    Does this answer your question? [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – Xavier Brassoud Oct 20 '21 at 10:53
  • 2
    Create a second array that defines the sort order -> [Javascript - sort array based on another array](https://stackoverflow.com/questions/13304543/javascript-sort-array-based-on-another-array) – Andreas Oct 20 '21 at 10:58
  • Without knowing any idea only I am just asking the doubt here. People simply give a negative rating for my question. If they know the answer just post here. Why some people doing this? – Hari Krishnan Oct 20 '21 at 11:06
  • _"People simply give a negative rating for my question. [...] Why some people doing this?"_ Because this is a bad question for Stack Overflow. Stack Overflow has some rules and one specific rule is _"Don't ask about... Questions you haven't tried to find an answer for (show your work!)"_ I assume the main idea behind this rule is to avoid Stack Overflow becoming a free code writing service and to avoid many duplicate questions. This question was probably asked and answered a dozen of times. – jabaa Oct 20 '21 at 11:08
  • May I know what is wrong with my question.? @ja – Hari Krishnan Oct 20 '21 at 11:14
  • _"Don't ask about... Questions you haven't tried to find an answer for (**show your work!**)"_ [tour] There is even an exclamation mark after _"show your work"_ Part of your work should be some research. It took Andreas 8 minutes to find a duplicate question. – jabaa Oct 20 '21 at 11:15
  • "*This question was probably asked and answered a dozen of times.*" ... and yet the two most helpful flagged comments, which where provided immediately after the OP was asking, entirely miss to refer to a solution which actually tackles the OP's real problem. There is a lack of effort on both sides; one side for maybe not trying hard enough and the other side not fully understanding the OP's true needs and thus not carefully enough searching for and linking to a helpful solution. – Peter Seliger Oct 20 '21 at 11:36
  • @PeterSeliger Doesn't https://stackoverflow.com/a/64048690/16540390 solve the problem? Couldn't this answer be copied and pasted? People linked to duplicate questions. The only more helpful step would be to directly link to the answers of this duplicate questions. – jabaa Oct 20 '21 at 11:46
  • @jabaa ... it does not. **1st** The links provided above did not target the specific answer you just referred to. Good luck for the one who struggles with a solution to find this low ranked (reputation of 12 versus 390, 95, 43...) and low listed (7th place) answer. **2nd** The solution is not advisable since it uses an array and not a direct lookup, thus, for retrieving the precedence values, each sorting step invokes `indexOf` twice (once for each item; and `indexOf` is based on iteration itself). – Peter Seliger Oct 20 '21 at 11:56
  • @PeterSeliger So actually it solves the problem but it's not the most efficient way. Wouldn't it be better to mark this question as a duplicate and add a better answer to the other question? Now we have a high ranked duplicate question with a worse solution and a low ranked question with an efficient answer. You could even copy and paste the title of the duplicate question. – jabaa Oct 20 '21 at 12:01
  • @jabaa ... I like the reasoning/argumentation. But then again I have to say neither the linked question/problem nor any of the provided answers/solutions/approaches comes close (and please everyone read all of it carefully) to the current OP's problem and the solution needed. – Peter Seliger Oct 20 '21 at 12:12
  • 2
    @PeterSeliger That given array is so small that those `.indexOf()` calls won't cause any harm. And you can be sure that any recent JS engine will optimize that into a direct lookup... SO is not a free code writing service. OP could have come up with a solution with the given links, or at least try one of the proposed answers -> [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – Andreas Oct 20 '21 at 13:15
  • @PeterSeliger The answer in my link to the question in Andreas' link is an exact solution to this question: https://jsfiddle.net/0gmqtcp3/. Your answer is almost a copy with an object instead of an array (which is only a performance improvement). Please elaborate why this answer doesn't come close to this question and why your answer does come close to this question. Do you really suggest to ask duplicate questions because some high ranked questions have too many answers and it's too much effort to find an easy-to-apply answer? – jabaa Oct 20 '21 at 13:43

1 Answers1

1

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; }
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37