Ivankooo1

test

Apr 3rd, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let countrys ={
  2.     Bulgaria: {
  3.         Sofia:{
  4.           population: 1242000
  5.         },
  6.         Plovdiv:{
  7.           population: 669796
  8.         }
  9.     },
  10.     Macedonia: {
  11.         Skopje:{
  12.           population: 627498
  13.         },
  14.         Ohrid:{
  15.             population: 42000
  16.         }
  17.  
  18.     }
  19. }
  20.  
  21. let sorted = Object.keys(countrys).sort((a,b)=>
  22.     getPop(countrys[b],'population') - getPop(countrys[a],'population') || a.localeCompare(b))
  23.  
  24. sorted.forEach(element=>{
  25.     console.log(`country: ${element}`);
  26.     let town = countrys[element];
  27.     let sort = Object.keys(town).sort((a,b)=>
  28.        town[a].population - town[b].population
  29.     )
  30.     sort.forEach(el=>{
  31.         console.log(`town: ${el} - population: ${town[el].population}`);
  32.     })
  33.     console.log('-'.repeat(50))
  34. })
  35.  
  36. function getPop(country,type){
  37.     let minNum = Number.MAX_SAFE_INTEGER;
  38.  
  39.     Object.keys(country).forEach(el=>{
  40.         let minPopulation = country[el][type];// ili countrys
  41.  
  42.         if(minNum > minPopulation){
  43.             minNum = minPopulation;
  44.         }
  45.     });    
  46.     return minNum;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment