/*
https://school.programmers.co.kr/learn/courses/30/lessons/12933

๋ฌธ์ œ ์„ค๋ช…
ํ•จ์ˆ˜ solution์€ ์ •์ˆ˜ n์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. n์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ํฐ๊ฒƒ๋ถ€ํ„ฐ ์ž‘์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ์ƒˆ๋กœ์šด ์ •์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ์„ธ์š”. ์˜ˆ๋ฅผ๋“ค์–ด n์ด 118372๋ฉด 873211์„ ๋ฆฌํ„ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ œํ•œ ์กฐ๊ฑด
n์€ 1์ด์ƒ 8000000000 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
*/

const solution = (n) => {
  const answer = Number(
    n
      // ์ธ์ž n์„ ๋ฌธ์ž์—ดํ™” ์‹œํ‚ด
      .toString()
      // ๋ฌธ์ž์—ด์„ ์ชผ๊ฐœ์„œ ๋ฐฐ์—ดํ™”
      .split('')
      // ์ชผ๊ฐ  ๋ฐฐ์—ด์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
      .sort((a, b) => b - a)
      // join ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋ฌธ์ž์—ด์„ ํ•ฉ์นจ
      .join('')
    // ์ตœ์ข…์ ์œผ๋กœ ๋ฌธ์ž์—ด ๊ฐ’์„ Number ๋ฉ”์„œ๋“œ๋กœ ์ •์ˆ˜ํ™”
  );

  return answer;
};

console.log(solution(118372));

์ฒ˜์Œ์—๋Š” ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž๋กœ ๋ฐฐ์—ด์„ ๋ถ„ํ•ดํ•ด์„œ ๋ฐ˜ํ™˜ํ•˜๋ ค๊ณ  ๋ณ„ ์ง“ ๋‹ค ํ•ด๋ณด๋‹ค๊ฐ€ ์•ˆ๋ผ์„œ.. join ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•ด ํ•ด๊ฒฐํ–ˆ๋‹ค. 

๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค!