Advertisement
Guest User

Untitled

a guest
Oct 8th, 2016
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function systemComponents(input) {
  2.     let systems = new Map()
  3.     input.forEach(row => {
  4.         let[system, component, subcomponent] = row.split(' | ')
  5.         if(!systems.has(system)) systems.set(system, {})
  6.         if(!systems.get(system).hasOwnProperty(component)) systems.get(system)[component] = []
  7.         systems.get(system)[component].push(subcomponent)
  8.     })
  9.     let systemsSortedKeys = [...systems.keys()].sort(amountOfComponentsThenAlpabeticalSort)
  10.     systemsSortedKeys.forEach(systemName => {
  11.         console.log(systemName)
  12.         let system = systems.get(systemName)
  13.         let componentsSortedKeys = Object.keys(system).sort((a, b) => {
  14.             return system[a].length < system[b].length
  15.         })
  16.         componentsSortedKeys.forEach(component => {
  17.             console.log(`|||${component}`)
  18.             system[component].forEach(subcomponent => {
  19.                 console.log(`||||||${subcomponent}`)
  20.             })
  21.         })
  22.     })
  23.  
  24.     function amountOfComponentsThenAlpabeticalSort(a, b) {
  25.         if(Object.keys(systems.get(a)).length === Object.keys(systems.get(b)).length) {
  26.             if(a > b) return 1
  27.             if(a < b) return -1
  28.         } else {
  29.             return Object.keys(systems.get(a)).length < Object.keys(systems.get(b)).length
  30.         }
  31.  
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement