Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input){
- let obj = {};
- for (const line of input) {
- const [top, mid, low] = line.split(' | ');
- if(!obj.hasOwnProperty(top)){
- obj[top] = {};
- obj[top][mid] = [];
- obj[top][mid].push(low);
- }else{
- if(!obj[top].hasOwnProperty(mid)){
- obj[top][mid]=[];
- }
- obj[top][mid].push(low);
- }
- }
- let result = '';
- Object.entries(obj).sort((a,b)=>{
- if(Object.keys(b[1]).length === Object.keys(a[1]).length){
- return a[0].localeCompare(b[0]);
- }
- return Object.keys(b[1]).length - Object.keys(a[1]).length;
- }).forEach(pair=>{
- result += `${pair[0]}\n`;
- Object.entries(pair[1]).sort((a,b)=>{
- return b[1].length - a[1].length;
- }).forEach(pair=>{
- result+=`|||${pair[0]}\n`
- pair[1].forEach(line=>{
- result+=`||||||${line}\n`;
- })
- })
- });
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment