ericagong / algorithm_solving

2 stars 0 forks source link

[정렬] 파일명 정렬 #62

Closed ericagong closed 1 year ago

ericagong commented 1 year ago

⭐ 성찰

  1. 정규 표현식 사용 const reg = /(\D*)([0-9]*)/i;
  2. String 빌트인 내장 객체 사용 String.prototype.match(regex) -> [ '12', index: 3, input: 'img12.png', groups: undefined ]

❓ 문제 상황

파일명 정렬

👨‍💻 문제 해결

✅ 1차 풀이: 정렬 함수를 제공하여 정렬

  1. 정규표현식으로 head, number, tail을 파싱
  2. 파싱한 배열을 원하는 정렬 함수를 작성하여, 정렬
    
    // https://school.programmers.co.kr/learn/courses/30/lessons/17686?language=javascript#

const regex = /[\d]{1,5}/; function solution(files) { const parsed = [];

files.forEach((file) => { const matched = file.match(regex);

const number = matched[0];
const head = file.slice(0, matched.index);
const tail = file.slice(matched.index + number.length);

parsed.push([head, number, tail, file]);

});

parsed.sort((a, b) => { let [ha, na, ta] = a; let [hb, nb, tb] = b;

// 1. 대소문자 구분 없이, 사전 오름차순 정렬
ha = ha.toUpperCase();
hb = hb.toUpperCase();
// if(ha !== hb) return hb < ha
if (ha !== hb) return ha < hb ? -1 : 1;
// if(ha > hb) return 1
// else if(ha < hb) return -1

// 2. 앞의 숫자 0은 무시하고 숫자 오름차순 정렬
na = Number(na);
nb = Number(nb);
if (na !== nb) return na - nb;

// 3. 나머지는 순서대로 정렬

});

// 이름만 반환 return parsed.map((v) => v[3]); }

### ✅ 2차 풀이: String.prototype.match() 사용
1. NUMBER regex 기준, head, number 도출
2. 주어진 정렬 방식에 따라 정렬
```javascript
function solution2(files) {
  const NUMBER = /\d{1,5}/;
  const result = [];
  files.forEach((file) => {
    const h = file.split(NUMBER)[0].toLowerCase();
    const n = Number(file.match(NUMBER)[0]);
    result.push([file, h, n]);
  });

  result.sort((a, b) => {
    if (a[1] > b[1]) return 1;
    else if (a[1] < b[1]) return -1;
    else {
      if (a[2] !== b[2]) return a[2] - b[2];
      else return 0;
    }
  });

  return result.map((info) => info[0]);
}