[프로그래머스 문제풀이28]복서 정렬하기 자바스크립트

매드갈릭레드망고·2021년 9월 16일
0
function solution(weights, head2head) {
    var answer = [];
    let boxers=[];
    const howManyBoxers=weights.length;
    weights.map((weight,idx)=>{
        const info=[idx+1,weight,head2head[idx]];
        return boxers.push(info)
    })
    boxers.map((info,idx)=>{
        let wins=0;
        let bigWins=0;
        let howManyBattles=0;
        const weight=info[1];
    
        for(let i=0;i<howManyBoxers;i++){
            if(info[2][i]!=="N")
                howManyBattles+=1;
            if(info[2][i]==="W"){     
                wins+=1;
                if(boxers[i][1]>weight)
                    bigWins+=1;        
            }
            
        }
        if(howManyBattles!=0)
        info.push(wins/howManyBattles);
        else
        info.push(0);
        info.push(bigWins);
        
        return info
    })
   .sort((a,b)=>{
       if(a[3]>b[3])
           return -1;
       if(a[3]<b[3])
           return 1;
       if(a[3]===b[3]){
           if(a[4]===b[4])
               {
                   return b[1]-a[1]
               }
           return b[4]-a[4]
       }
    })
    .map(val=>{
        return answer.push(val[0]);
    })
    console.log(boxers)
    return answer;
}

sortfunction을 잘 구현해야한다.

0개의 댓글