알고리즘 - [3차] 파일명 정렬

HoJeong Im·2021년 9월 23일
0

Break_Algo

목록 보기
15/46

문제

코드

function solution(files) {
    var answer = [];
    let testing = []; 
    files.forEach((file,k)=>{
        testing.push([k,...file.split(/([0-9]+)/)]);
    });
    console.log(testing)
    
    testing = testing.sort((file1, file2)=>{

        let test1Upper = file1[1].toUpperCase();
        let test2Upper = file2[1].toUpperCase();
        
        if(test1Upper !== test2Upper){
            
 console.log(test1Upper, test2Upper)
            if(test1Upper < test2Upper){
                return -1;
            }
            
            else if(test1Upper > test2Upper){
                return 1;
            }
        }
        else {
            let num1 = parseInt(file1[2].substring(0,5)) 
            let num2 = parseInt(file2[2].substring(0,5))
            if(num1 !== num2){
                return num1-num2;
            }
            else {
                return file1[0]-file2[0];
            }
        }
    
    });
    console.log(testing);
    
    return testing.map((item)=>{
        item.shift();
        return item.join("");
    });
}

회고

profile
꾸준함이 제일 빠른 길이었다

0개의 댓글