Advertisement
bai_onzi

armyLunch.js

Jun 29th, 2023
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //s - sergeant; c - corporal; p - private
  2.  
  3. const n = +gets();
  4. const soldiers = gets().split(' ');
  5.  
  6. const sortedSoldiers = (soldiers) => {
  7.     const rankOrder = { S: 0, C: 1, P: 2 };
  8.  
  9.     soldiers.sort((a, b) => {
  10.         const rankA = a.charAt(0);
  11.         const rankB = b.charAt(0);
  12.  
  13.         if(rankOrder[rankA] < rankOrder[rankB]){
  14.             return -1;
  15.         }else if(rankOrder[rankA] > rankOrder[rankB]){
  16.             return 1;
  17.         }else{
  18.             return 0;
  19.         }
  20.     });
  21.     return soldiers;
  22. };
  23. console.log(sortedSoldiers(soldiers).join(' '))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement