maymunskoa

SystemComponents /Objects and Classes/

Jul 4th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.    
  3.     let obj = {};
  4.     for (const line of input) {
  5.         const [top, mid, low] = line.split(' | ');
  6.         if(!obj.hasOwnProperty(top)){
  7.             obj[top] = {};
  8.             obj[top][mid] = [];
  9.             obj[top][mid].push(low);
  10.         }else{
  11.             if(!obj[top].hasOwnProperty(mid)){
  12.                 obj[top][mid]=[];
  13.             }
  14.             obj[top][mid].push(low);
  15.         }
  16.     }
  17.     let result = '';
  18.     Object.entries(obj).sort((a,b)=>{
  19.         if(Object.keys(b[1]).length === Object.keys(a[1]).length){
  20.             return a[0].localeCompare(b[0]);
  21.         }
  22.         return Object.keys(b[1]).length - Object.keys(a[1]).length;
  23.     }).forEach(pair=>{
  24.         result += `${pair[0]}\n`;
  25.         Object.entries(pair[1]).sort((a,b)=>{
  26.             return b[1].length - a[1].length;
  27.         }).forEach(pair=>{
  28.             result+=`|||${pair[0]}\n`
  29.             pair[1].forEach(line=>{
  30.                 result+=`||||||${line}\n`;
  31.             })
  32.         })        
  33.     });
  34.    
  35. return result;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment