
지정된 우선순위 문자열 order에 따라 문자열 s를 정렬하는 문제.
order에 포함되지 않은 문자들은 어느 위치에 오든 상관없음.
order 문자를 Key, 인덱스(순서)를 Value로 저장하여 시간에 우선순위를 조회하도록 구성.s를 배열로 변환 후 toSorted()를 사용해 정렬.order에 없는 문자는 가장 큰 인덱스값(order.length)을 부여해 뒤로 배치.join('')으로 다시 문자열로 합성하여 반환.function customSortString(order: string, s: string): string {
const orderLen = order.length;
const priorityMap = new Map<string, number>();
// 1. order의 각 문자에 우선순위(인덱스) 부여
for (let i = 0; i < orderLen; i++) {
priorityMap.set(order[i], i);
}
// 2. 우선순위를 기준으로 정렬
const sortedArr = [...s].toSorted((a, b) => {
const priorityA = priorityMap.get(a) ?? orderLen;
const priorityB = priorityMap.get(b) ?? orderLen;
return priorityA - priorityB;
});
// 3. 정렬된 배열을 문자열로 변환
return sortedArr.join('');
}