0
let Card=['Primary',Secondary,'Abccc','BBC','DDC']

How can I sort the given array based on where if the array contains the Values 'Primary' and 'Secondary', we need to order in such way that "Primary" is on top and "Secondary" below that irrespective of the casing?

I just have an array which has dynamic data and incase the values 'Primary' or 'Secondary' comes in the array then I need to push the 'Primary' to 0 index and 'Secondary' to 1 index of the array.

Terry
  • 63,248
  • 15
  • 96
  • 118
rock rake
  • 5
  • 1

1 Answers1

0

This works

let Card=['Primary','Secondary','Abccc','BBC','DDC'].sort(() => Math.random()*2 - 1)
console.log(Card.join());
Card.sort((a, b) => {
  if ( a === 'Primary' ) return -1;
  if ( b === 'Primary' ) return 1;
  if ( a === 'Secondary' ) return -1;
  if ( b === 'Secondary' ) return 1;
  return a.localeCompare(b);
});
console.log(Card.join());
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87