html
JavaScript Sort Method ์ดํดํ๊ธฐ: ์ข ํฉ ๊ฐ์ด๋
๋ชฉ์ฐจ
- ์๊ฐ ........................................................................ 1
- ๊ธฐ๋ณธ Sort Method ........................................ 3
- JavaScript์์ ์ซ์ ์ ๋ ฌ ......................... 6
- Sort Method๋ฅผ ์ด์ฉํ ๊ฐ์ฒด ์ ๋ ฌ ............................................................ 10
- ์ํ๋ฒณ ์ ๋ ฌ ๋ฐ ๋์๋ฌธ์ ๊ตฌ๋ถ ............................................................... 15
- ๊ณ ๊ธ ์ ๋ ฌ ๊ธฐ๋ฒ ๋ฐ ์ต์ ํ ............................................................ 20
- ๊ฒฐ๋ก ........................................................................ 25
์๊ฐ
์ ๋ ฌ์ ํ๋ก๊ทธ๋๋ฐ์์ ๊ธฐ๋ณธ์ ์ธ ์ฐ์ฐ์ผ๋ก, ๋ฐ์ดํฐ๋ฅผ ์๋ฏธ ์๋ ๋ฐฉ์์ผ๋ก ์กฐ์งํ๋ ๋ฐ ํ์์ ์ ๋๋ค. JavaScript์์ sort method๋ ๋ฐฐ์ด ๋ด์ ์์๋ฅผ ๋ฐฐ์ดํ๋ ๋ค๋ชฉ์ ์ ๊ทผ ๋ฐฉ์์ ์ ๊ณตํฉ๋๋ค. ์ด ์ ์์ฑ ์ JavaScript sort method์ ๋ณต์ก์ฑ์ ํ๊ณ ๋ค์ด ๊ธฐ๋ณธ ๋์, ์ซ์ ๋ฐ ๊ฐ์ฒด๋ฅผ ์ํ ์ ๋ ฌ ๋ฉ์ปค๋์ฆ ์ปค์คํฐ๋ง์ด์ง, ๋์๋ฌธ์ ๊ตฌ๋ถ์ด ์๋ ์ํ๋ฒณ ์ฒ๋ฆฌ, ๊ทธ๋ฆฌ๊ณ ๊ณ ๊ธ ์ต์ ํ ๊ธฐ๋ฒ์ ํ๊ตฌํฉ๋๋ค.
์ฃผ์ ๋ด์ฉ:
- JavaScript์ ๊ธฐ๋ณธ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ ์ดํด
- ๋ค์ํ ๋ฐ์ดํฐ ํ์ ์ ์ํ sort method ์ปค์คํฐ๋ง์ด์ง
- ์ํ๋ฒณ ์ ๋ ฌ ์ ๋์๋ฌธ์ ๊ตฌ๋ถ ์ฒ๋ฆฌ
- ์ฑ๋ฅ๊ณผ ์ ๋ขฐ์ฑ์ ์ํ ์ ๋ ฌ ์ต์ ํ
JavaScript Sort Method์ ์ฅ๋จ์ :
์ฅ์ | ๋จ์ |
---|---|
๊ฐ๋จํ ๋ฌธ๋ฒ์ผ๋ก ๊ตฌํ์ด ์ฉ์ด | ๊ธฐ๋ณธ ์ ๋ ฌ์ด ์์์น ๋ชปํ ๊ฒฐ๊ณผ๋ฅผ ์ด๋ํ ์ ์์ |
๋น๊ต ํจ์๋ก ๋์ ์ปค์คํฐ๋ง์ด์ง ๊ฐ๋ฅ | ๊ฐ์ฒด ์ ๋ ฌ์๋ ๋ช ์์ ์ธ ๋น๊ต ๋ก์ง์ด ํ์ํจ |
๋ค์ํ ๋ฐ์ดํฐ ํ์ ์ ํจ๊ณผ์ ์ผ๋ก ์ ๋ ฌ ๊ฐ๋ฅ | ๋์๋ฌธ์ ๊ตฌ๋ถ์ด ์ํ๋ฒณ ์ ๋ ฌ์ ์ํฅ์ ๋ฏธ์น ์ ์์ |
JavaScript Sort๋ฅผ ์ฌ์ฉํ๋ ์๊ธฐ์ ์ฅ์:
- ๋ฐ์ดํฐ ํํ: ์ฌ์ฉ์ ๊ฐ๋ ์ฑ์ ๋์ด๊ธฐ ์ํด ๋ชฉ๋ก์ด๋ ํ ์ด๋ธ ์ ๋ฆฌ
- ์๊ณ ๋ฆฌ์ฆ ์ต์ ํ: ๋ค๋ฅธ ์๊ณ ๋ฆฌ์ฆ์ ํจ์จ์ฑ์ ๋์ด๊ธฐ ์ํด ๋ฐ์ดํฐ ์ฌ์ ์ ๋ ฌ
- ๋ฐ์ดํฐ ๋ถ์: ๋ ๋์ ์ธ์ฌ์ดํธ๋ฅผ ์ํ ์ซ์ ๋๋ ๋ฒ์ฃผํ ๋ฐ์ดํฐ ์ ๋ ฌ
๊ธฐ๋ณธ Sort Method
JavaScript์ sort method๋ ๋ฐฐ์ด์ ์์๋ฅผ ๋ฐฐ์ดํ๋๋ก ์ค๊ณ๋์์ต๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฌธ์์ด๋ก ์์๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํฉ๋๋ค. ์ด ๋์์ ํนํ ์ซ์ ๋ฐ์ดํฐ์ ํจ๊ป ์ฌ์ฉํ ๋ ์์์น ๋ชปํ ์ ๋ ฌ ๊ฒฐ๊ณผ๋ฅผ ์ด๋ํ ์ ์์ต๋๋ค.
๊ธฐ๋ณธ Sort์ ๋์ ๋ฐฉ์
๋ค์ ๋ฐฐ์ด์ ๊ณ ๋ คํด๋ณด์ธ์:
1 2 3 4 |
<!-- let numbers = [0, 15, 5, 27, 3, 10, 12, 25]; console.log(numbers); // Output: [0, 15, 5, 27, 3, 10, 12, 25] --> |
sort method๊ฐ ๋น๊ต ํจ์ ์์ด ์ ์ฉ๋ ๋:
1 2 3 4 |
<!-- numbers.sort(); console.log(numbers); // Output: [0, 10, 12, 15, 25, 27, 3, 5] --> |
์ค๋ช :
- sort method๋ ๊ฐ ์ซ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํฉ๋๋ค.
- ๋ฌธ์์ด์ ์ฒซ ๋ฒ์งธ ๋ฌธ์๋ฅผ ๋น๊ตํ์ฌ ์ ๋ ฌ ์์๋ฅผ ๊ฒฐ์ ํฉ๋๋ค.
- ์ด๋ก ์ธํด ์ซ์๊ฐ ์๋ ๋ฌธ์์ด์ ์ด๊ธฐ ์๋ฆฟ์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ๋ฉ๋๋ค.
๊ธฐ๋ณธ Sort์ ๋ฌธ์ ์
- ์ซ์ ์ ๋ ฌ ๋ฌธ์ : ์๋ฆฟ์๊ฐ ๋ค๋ฅธ ์ซ์๋ ์๋ชป๋ ์์๋ก ์ ๋ ฌ๋ ์ ์์ต๋๋ค.
- ์๋ณธ ๋ฐฐ์ด ๋ณ์กฐ: sort method๋ ์๋ณธ ๋ฐฐ์ด์ ์์ ํ๋ฏ๋ก ํญ์ ๋ฐ๋์งํ์ง ์์ ์ ์์ต๋๋ค.
์์:
์๋ณธ ๋ฐฐ์ด | ๊ธฐ๋ณธ Sort ํ |
---|---|
[0, 15, 5, 27, 3] | [0, 10, 12, 15, 25, 27, 3, 5] |
ํต์ฌ ์์ : ๊ธฐ๋ณธ sort method๋ ์ง๊ด์ ์ด์ง๋ง, ๋ฌธ์์ด ๊ธฐ๋ฐ ๋น๊ต๋ก ์ธํด ์ซ์๋ฅผ ์์๋๋ก ์ ๋ ฌํ์ง ๋ชปํ ์ ์์ต๋๋ค.
JavaScript์์ ์ซ์ ์ ๋ ฌ
์ ํํ ์์น ์ ๋ ฌ์ ์ํด์๋ sort method์ ๋น๊ต ํจ์๋ฅผ ์ ๊ณตํ๋ ๊ฒ์ด ํ์์ ์ ๋๋ค. ์ด ํจ์๋ ๋ฌธ์์ด ํํ์ด ์๋ ์์น ๊ฐ์ ๊ธฐ๋ฐ์ผ๋ก ์ ๋ ฌ ์์๋ฅผ ์ ์ํฉ๋๋ค.
๋น๊ต ํจ์ ๊ตฌํ
๋น๊ต ํจ์๋ ๋ ์์(a
์ b
)๋ฅผ ๋น๊ตํ์ฌ ์ ๋ ฌ ์์๋ฅผ ๊ฒฐ์ ํฉ๋๋ค. ๋ค์์ ์ซ์๋ฅผ ์ค๋ฆ์ฐจ์ ๋ฐ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ ๋ฐฉ๋ฒ์
๋๋ค:
์ค๋ฆ์ฐจ์
1 2 3 4 |
<!-- numbers.sort((a, b) => a - b); console.log(numbers); // Output: [0, 3, 5, 10, 12, 15, 25, 27] --> |
์ค๋ช :
a - b
๊ฐ ์์์ด๋ฉดa
๊ฐb
๋ณด๋ค ๋จผ์ ์ต๋๋ค.a - b
๊ฐ ์์์ด๋ฉดa
๊ฐb
๋ณด๋ค ๋์ค์ ์ต๋๋ค.a - b
๊ฐ 0์ผ ๊ฒฝ์ฐ, ์์๊ฐ ๋ณ๊ฒฝ๋์ง ์์ต๋๋ค.
๋ด๋ฆผ์ฐจ์
1 2 3 4 |
<!-- numbers.sort((a, b) => b - a); console.log(numbers); // Output: [27, 25, 15, 12, 10, 5, 3, 0] --> |
์ค๋ช :
- ๋น๊ต ํจ์๋ฅผ ๋ฐ์ ์์ผ(
b - a
) ๋ฐฐ์ด์ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํฉ๋๋ค.
์ฃผ์์ด ํฌํจ๋ ์ฝ๋ ์์
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!-- let numbers = [0, 15, 5, 27, 3, 10, 12, 25]; // ์ค๋ฆ์ฐจ์ ์ ๋ ฌ numbers.sort((a, b) => { // ๋ ์ซ์ ๋น๊ต return a - b; }); console.log(numbers); // Output: [0, 3, 5, 10, 12, 15, 25, 27] // ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ numbers.sort((a, b) => { // ๋ด๋ฆผ์ฐจ์์ ์ํ ๋น๊ต ๋ฐ์ return b - a; }); console.log(numbers); // Output: [27, 25, 15, 12, 10, 5, 3, 0] --> |
๊ฐ๋จํ ๋น๊ต ํจ์
๊ฐ๊ฒฐ์ฑ์ ์ํด ํ์ดํ ํจ์ ์ถ์ฝํ์ ์ฌ์ฉํ ์ ์์ต๋๋ค:
1 2 3 4 5 6 7 |
<!-- // ์ค๋ฆ์ฐจ์ ์ ๋ ฌ numbers.sort((a, b) => a - b); // ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ numbers.sort((a, b) => b - a); --> |
์ถ๋ ฅ ์ค๋ช
์ค๋ฆ์ฐจ์ ์ ๋ ฌ์ ์ ์ฉํ ํ:
- ์ด์ : [0, 15, 5, 27, 3, 10, 12, 25]
- ์ดํ: [0, 3, 5, 10, 12, 15, 25, 27]
๊ฐ ์ซ์๊ฐ ์์น ๊ฐ์ ๊ธฐ์ค์ผ๋ก ๋ฐฐ์ด๋์ด ์ ํํ ์ ๋ ฌ์ ๋ณด์ฅํฉ๋๋ค.
Sort Method๋ฅผ ์ด์ฉํ ๊ฐ์ฒด ์ ๋ ฌ
๊ฐ์ฒด ๋ฐฐ์ด์ ์ ๋ ฌํ๋ ค๋ฉด, ์ด๋ค ๊ฐ์ฒด ์์ฑ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ ์ง ๋ช ์ํ๋ ๋น๊ต ํจ์๋ฅผ ์ ์ํด์ผ ํฉ๋๋ค. ์ด๋ ๋ณต์กํ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์ ๋ ฌํ๋ ๋ฐ ์ ์ฐ์ฑ์ ์ ๊ณตํฉ๋๋ค.
์์ ์๋๋ฆฌ์ค
๋ค์์ ์ ํ ๊ฐ์ฒด ๋ฐฐ์ด์ ๊ณ ๋ คํด๋ณด์ธ์:
1 2 3 4 5 6 7 8 |
<!-- let products = [ { name: 'Shoe', price: 50 }, { name: 'Shirt', price: 20 }, { name: 'Pants', price: 35 }, { name: 'Bag', price: 45 } ]; --> |
๊ฐ๊ฒฉ ๋ณ ์ ๋ ฌ (์ค๋ฆ์ฐจ์)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!-- products.sort((a, b) => a.price - b.price); console.log(products); /* Output: [ { name: 'Shirt', price: 20 }, { name: 'Pants', price: 35 }, { name: 'Bag', price: 45 }, { name: 'Shoe', price: 50 } ] */ --> |
์ค๋ช :
- ๋น๊ต ํจ์๋ ๊ฐ ๊ฐ์ฒด์
price
์์ฑ์ ๋น๊ตํฉ๋๋ค. - ์ด๋ฅผ ํตํด ์ ํ์ด ๋ฎ์ ๊ฐ๊ฒฉ๋ถํฐ ๋์ ๊ฐ๊ฒฉ ์์ผ๋ก ์ ๋ ฌ๋ฉ๋๋ค.
๊ฐ๊ฒฉ ๋ณ ์ ๋ ฌ (๋ด๋ฆผ์ฐจ์)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!-- products.sort((a, b) => b.price - a.price); console.log(products); /* Output: [ { name: 'Shoe', price: 50 }, { name: 'Bag', price: 45 }, { name: 'Pants', price: 35 }, { name: 'Shirt', price: 20 } ] */ --> |
์ค๋ช :
- ๋น๊ต ํจ์๋ฅผ ๋ฐ์ ์์ผ ์ ํ์ ๋์ ๊ฐ๊ฒฉ๋ถํฐ ๋ฎ์ ๊ฐ๊ฒฉ ์์ผ๋ก ์ ๋ ฌํฉ๋๋ค.
์ข ํฉ ์ฝ๋ ์์
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<!-- let products = [ { name: 'Shoe', price: 50 }, { name: 'Shirt', price: 20 }, { name: 'Pants', price: 35 }, { name: 'Bag', price: 45 } ]; // ๊ฐ๊ฒฉ ๊ธฐ์ค ์ค๋ฆ์ฐจ์ ์ ๋ ฌ products.sort((a, b) => { if (a.price > b.price) { return 1; // ์์ ์ ์ง } else if (a.price < b.price) { return -1; // ์์ ๋ณ๊ฒฝ } else { return 0; // ๋ณ๊ฒฝ ์์ } }); console.log(products); // ๊ฐ๊ฒฉ ๊ธฐ์ค ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ products.sort((a, b) => { return b.price - a.price; }); console.log(products); --> |
์ถ๋ ฅ ์ค๋ช
- ์ค๋ฆ์ฐจ์ ์ ๋ ฌ:
- ์ ํ์ด ๊ฐ์ฅ ์ ๋ ดํ ๊ฐ๊ฒฉ๋ถํฐ ๊ฐ์ฅ ๋น์ผ ๊ฐ๊ฒฉ ์์ผ๋ก ๋ฐฐ์ด๋ฉ๋๋ค.
- ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ:
- ์ ํ์ด ๊ฐ์ฅ ๋น์ผ ๊ฐ๊ฒฉ๋ถํฐ ๊ฐ์ฅ ์ ๋ ดํ ๊ฐ๊ฒฉ ์์ผ๋ก ๋ฐฐ์ด๋ฉ๋๋ค.
๋ณต์กํ ์ ๋ ฌ ์ฒ๋ฆฌ
์ฌ๋ฌ ์์ฑ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๋ ๊ฒ๊ณผ ๊ฐ์ ๋ ๋ณต์กํ ์ ๋ ฌ ์๊ตฌ์ฌํญ์ ์ํด ๋น๊ต ํจ์๋ฅผ ํ์ฅํ ์ ์์ต๋๋ค.
์์: ๊ฐ๊ฒฉ๊ณผ ์ด๋ฆ ๊ธฐ์ค ์ ๋ ฌ
1 2 3 4 5 6 7 8 9 |
<!-- products.sort((a, b) => { if (a.price === b.price) { return a.name.localeCompare(b.name); } return a.price - b.price; }); console.log(products); --> |
์ค๋ช :
- ๊ฐ์ ๊ฐ๊ฒฉ์ ์ ํ์
name
์ ๊ธฐ์ค์ผ๋ก ์ํ๋ฒณ ์์ผ๋ก ์ถ๊ฐ ์ ๋ ฌ๋ฉ๋๋ค.
์ํ๋ฒณ ์ ๋ ฌ ๋ฐ ๋์๋ฌธ์ ๊ตฌ๋ถ
JavaScript์์ ๋ฌธ์์ด์ ์ ๋ ฌํ๋ ๊ฒ์ sort method๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๋จํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ๋์๋ฌธ์ ๊ตฌ๋ถ์ ์ ๋ ฌ ์์๋ฅผ ๊ฒฐ์ ํ๋ ๋ฐ ์ค์ํ ์ญํ ์ ํ๋ฏ๋ก ์์์น ๋ชปํ ๊ฒฐ๊ณผ๋ฅผ ์ด๋ํ ์ ์์ต๋๋ค.
์ด๋ฆ ๋ฐฐ์ด ์ ๋ ฌ
๋ค์ ์ด๋ฆ ๋ฐฐ์ด์ ๊ณ ๋ คํด๋ณด์ธ์:
1 2 3 |
<!-- let names = ['John', 'jackie', 'Alex', 'Sara', 'Pooja']; --> |
๊ธฐ๋ณธ Sort ๋์
1 2 3 4 |
<!-- names.sort(); console.log(names); // Output: ['Alex', 'John', 'Pooja', 'Sara', 'jackie'] --> |
์ค๋ช :
- sort method๋ ๋ฌธ์์ด์ UTF-16 ์ฝ๋ ๋จ์๋ก ๋น๊ตํฉ๋๋ค.
- ๋๋ฌธ์๋ ์๋ฌธ์๋ณด๋ค ๋ฎ์ ์ฝ๋ ๋จ์ ๊ฐ์ ๊ฐ์ง๋ฏ๋ก ๋จผ์ ์ ๋ ฌ๋ฉ๋๋ค.
๋์๋ฌธ์ ๊ตฌ๋ถ ๋ฌธ์
๋ชจ๋ ์ด๋ฆ์ด ์๋ฌธ์์ธ ๊ฒฝ์ฐ, ์ ๋ ฌ ๋์์ด ๋ค๋ฅด๊ฒ ๋ํ๋ฉ๋๋ค:
1 2 3 4 5 |
<!-- let lowercaseNames = ['john', 'jackie', 'alex', 'sara', 'pooja']; lowercaseNames.sort(); console.log(lowercaseNames); // Output: ['alex', 'jackie', 'john', 'pooja', 'sara'] --> |
์ค๋ช :
- ๋ชจ๋ ์ด๋ฆ์ด ์๋ฌธ์์ผ ๊ฒฝ์ฐ, ๋์๋ฌธ์ ๊ด๋ จ ๋ถ์ผ์น ์์ด ์์ํ ์ํ๋ฒณ ์์ผ๋ก ์ ๋ ฌ๋ฉ๋๋ค.
๋์๋ฌธ์ ๊ตฌ๋ถ ์๋ ์ ๋ ฌ ๊ตฌํ
๋์๋ฌธ์ ๊ตฌ๋ถ ์๋ ์ ๋ ฌ์ ์ํด ๋น๊ต ํจ์ ๋ด์์ ๋ชจ๋ ๋ฌธ์์ด์ ๋์ผํ ๋์๋ฌธ์๋ก ๋ณํํฉ๋๋ค.
1 2 3 4 |
<!-- names.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())); console.log(names); // Output: ['Alex', 'jackie', 'John', 'Pooja', 'Sara'] --> |
์ค๋ช :
toLowerCase()
๋ ๋ ๋ฌธ์์ด์ ์๋ฌธ์๋ก ๋ณํํฉ๋๋ค.localeCompare
๋ ์ง์ญ์ ๋ง๋ ์ ํํ ์ํ๋ฒณ ๋น๊ต๋ฅผ ๋ณด์ฅํฉ๋๋ค.
์ข ํฉ ์ฝ๋ ์์
1 2 3 4 5 6 7 8 9 10 11 |
<!-- let names = ['John', 'jackie', 'Alex', 'Sara', 'Pooja']; // ๊ธฐ๋ณธ ์ ๋ ฌ (๋์๋ฌธ์ ๊ตฌ๋ถ) names.sort(); console.log('Default Sort:', names); // ๋์๋ฌธ์ ๊ตฌ๋ถ ์๋ ์ ๋ ฌ names.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())); console.log('Case-Insensitive Sort:', names); --> |
์ถ๋ ฅ
1 2 3 |
Default Sort: [ 'Alex', 'John', 'Pooja', 'Sara', 'jackie' ] Case-Insensitive Sort: [ 'Alex', 'jackie', 'John', 'Pooja', 'Sara' ] |
์ํ๋ฒณ ์ ๋ ฌ์ ์ฃผ์ ์ฌํญ
- ๊ธฐ๋ณธ ์ ๋ ฌ์ ๋์๋ฌธ์ ๊ตฌ๋ถ: ๋๋ฌธ์๊ฐ ์๋ฌธ์๋ณด๋ค ์ฐ์ ๋ฉ๋๋ค.
- ๋์๋ฌธ์ ๊ตฌ๋ถ ์๋ ์ ๋ ฌ์ ์ํ ๋น๊ต ํจ์ ์ฌ์ฉ: ์ง์ ํ ์ํ๋ฒณ ์์๋ฅผ ๋ณด์ฅ
localeCompare
๋ฉ์๋: ๋ก์ผ์ผ์ ๊ธฐ๋ฐํ ์ ํํ ๋ฌธ์์ด ๋น๊ต ์ ๊ณต
๊ณ ๊ธ ์ ๋ ฌ ๊ธฐ๋ฒ ๋ฐ ์ต์ ํ
๊ธฐ๋ณธ ์ ๋ ฌ์ ๋์ด, ๊ณ ๊ธ ๊ธฐ๋ฒ์ ์ฑ๋ฅ์ ํฅ์์ํค๊ณ ๋์ฉ๋ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ฉฐ ๋ ์ ๊ตํ ์ ๋ ฌ ๊ธฐ๋ฅ์ ์ ๊ณตํ ์ ์์ต๋๋ค.
Sort์ Reverse ๋ฉ์๋ ์ฒด์ด๋
JavaScript๋ ๋ฉ์๋ ์ฒด์ด๋์ ํ์ฉํ์ฌ ํ๋์ ๋ฌธ์ฅ ๋ด์์ ์ฌ๋ฌ ์์ ์ ์ํํ ์ ์์ต๋๋ค.
์์: ์ค๋ฆ์ฐจ์ ์ ๋ ฌ ํ ์ญ์ ์ ๋ ฌ
1 2 3 4 5 |
<!-- let numbers = [0, 15, 5, 27, 3, 10, 12, 25]; numbers.sort((a, b) => a - b).reverse(); console.log(numbers); // Output: [27, 25, 15, 12, 10, 5, 3, 0] --> |
์ค๋ช :
- ๋จผ์ , ๋ฐฐ์ด์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํฉ๋๋ค.
- ๊ทธ ๋ค์, reverse method๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์ ๋ด๋ฆผ์ฐจ์์ผ๋ก ๋ฐ์ ์ํต๋๋ค.
์ ๋ ฌ์ ์์ ์ฑ
์์ ์ ์ธ ์ ๋ ฌ์ ๋๋ฑํ ์์์ ์๋์ ์์๋ฅผ ์ ์งํฉ๋๋ค. JavaScript์ sort method๋ ๋ชจ๋ ์์ง์์ ์์ ์ ์ธ ๊ฒ์ด ๋ณด์ฅ๋์ง ์์ต๋๋ค.
์์ ์ฑ ๋ณด์ฅ ๋ฐฉ๋ฒ:
- ์์์ ์๋ณธ ์ธ๋ฑ์ค ์ถ๊ฐ: ๊ฐ์ด ๋์ผํ ๋ ์์๋ฅผ ์ ์งํ๋ ๋ฐ ๋์์ด ๋ฉ๋๋ค.
- ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ: Lodash์ ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์์ ์ ์ธ ์ ๋ ฌ ํจ์๋ฅผ ์ ๊ณตํฉ๋๋ค.
์ฑ๋ฅ ๊ณ ๋ ค ์ฌํญ
๋์ฉ๋ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฌํ๋ ๊ฒ์ ์ฑ๋ฅ์ ๋ถ๋ด์ ์ค ์ ์์ต๋๋ค. ์ ๋ ฌ ์์ ์ ์ต์ ํํ๋ ๊ฒ์ ํจ์จ์ฑ์ ๋์ด๋ ๋ฐ ์ค์ํฉ๋๋ค.
ํ:
- ์ ์ ํ ์๊ณ ๋ฆฌ์ฆ ์ ํ: ํน์ ๊ฒฝ์ฐ์ ํน์ ์๊ณ ๋ฆฌ์ฆ์ด ๋ ๋์ ์ฑ๋ฅ์ ๋ณด์ผ ์ ์์ต๋๋ค.
- ๋น๊ต ์ต์ํ: ๋น๊ต ํจ์ ๋ด์ ์ฐ์ฐ ์๋ฅผ ์ค์ ๋๋ค.
- ๋ถํ์ํ ์ ๋ ฌ ํผํ๊ธฐ: ์ ๋ ฌ์ด ํ์์ ์ธ์ง ํ์ธํ ํ ์ํํฉ๋๋ค.
๋ณต์กํ ๋ฐ์ดํฐ ๊ตฌ์กฐ ์ฒ๋ฆฌ
์ค์ฒฉ๋ ๊ฐ์ฒด๋ ๋ค์ค ์ ๋ ฌ ๊ธฐ์ค์ ๋ค๋ฃฐ ๋๋ ํ์ฅ๋ ๋น๊ต ํจ์๊ฐ ํ์ํฉ๋๋ค.
์์: ๋ค์ค ์์ฑ ๊ธฐ์ค ์ ๋ ฌ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<!-- let employees = [ { name: 'John', age: 30, department: 'HR' }, { name: 'Jane', age: 25, department: 'Finance' }, { name: 'John', age: 22, department: 'IT' }, { name: 'Alex', age: 30, department: 'Marketing' } ]; employees.sort((a, b) => { if (a.name === b.name) { return a.age - b.age; } return a.name.localeCompare(b.name); }); console.log(employees); /* Output: [ { name: 'Alex', age: 30, department: 'Marketing' }, { name: 'Jane', age: 25, department: 'Finance' }, { name: 'John', age: 22, department: 'IT' }, { name: 'John', age: 30, department: 'HR' } ] */ --> |
์ค๋ช :
- ๋จผ์ , ์ง์ ์ด๋ฆ์ ๊ธฐ์ค์ผ๋ก ์ํ๋ฒณ ์์ผ๋ก ์ ๋ ฌ๋ฉ๋๋ค.
- ์ด๋ฆ์ด ๋์ผํ ๊ฒฝ์ฐ, ๋์ด๋ฅผ ๊ธฐ์ค์ผ๋ก ์ถ๊ฐ ์ ๋ ฌ๋ฉ๋๋ค.
ํฅ์๋ ์ ๋ ฌ์ ์ํ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํ์ฉ
Lodash์ Underscore.js์ ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ๋ณต์กํ ์๋๋ฆฌ์ค๋ฅผ ์ฝ๊ฒ ์ฒ๋ฆฌํ ์ ์๋ ๊ณ ๊ธ ์ ๋ ฌ ์ ํธ๋ฆฌํฐ๋ฅผ ์ ๊ณตํฉ๋๋ค.
Lodash๋ฅผ ์ด์ฉํ ์์:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!-- const _ = require('lodash'); let users = [ { 'user': 'fred', 'age': 48 }, { 'user': 'barney', 'age': 34 }, { 'user': 'fred', 'age': 40 }, { 'user': 'barney', 'age': 36 } ]; let sortedUsers = _.sortBy(users, ['user', 'age']); console.log(sortedUsers); /* Output: [ { user: 'barney', age: 34 }, { user: 'barney', age: 36 }, { user: 'fred', age: 40 }, { user: 'fred', age: 48 } ] */ --> |
์ฅ์ :
- ์ฌ์ฉ ์ฉ์ด์ฑ: ๋ณต์กํ ์ ๋ ฌ ์์ ์ ๋จ์ํํฉ๋๋ค.
- ๊ธฐ๋ฅ ํฅ์: ๋ฐ์ดํฐ ์กฐ์์ ์ํ ์ถ๊ฐ ์ ํธ๋ฆฌํฐ ์ ๊ณต
- ์ฑ๋ฅ ์ต์ ํ: ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์๋์ ํจ์จ์ฑ์ ์ํด ์ต์ ํ๋์ด ์๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค.
๊ฒฐ๋ก
JavaScript sort method๋ ์ซ์, ๋ฌธ์์ด ๋๋ ๊ฐ์ฒด๋ก ๊ตฌ์ฑ๋ ๋ฐฐ์ด ์์๋ฅผ ์กฐ์งํ๋ ๊ฐ๋ ฅํ ๋๊ตฌ์ ๋๋ค. ๊ธฐ๋ณธ ๋์์ ์ดํดํ๊ณ ๋น๊ต ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฅผ ์ปค์คํฐ๋ง์ด์งํ๋ ๋ฐฉ๋ฒ์ ์๋ ๊ฒ์ ์ ํํ๊ณ ํจ์จ์ ์ธ ๋ฐ์ดํฐ ์กฐ์์ ์ํด ํ์์ ์ ๋๋ค. ๋์๋ฌธ์ ๊ตฌ๋ถ๊ณผ ๊ฐ์ ๋ฏธ๋ฌํ ์ฐจ์ด๋ฅผ ๋ค๋ฃจ๊ณ ๊ณ ๊ธ ๊ธฐ๋ฒ์ ์ฌ์ฉํจ์ผ๋ก์จ, ๊ฐ๋ฐ์๋ JavaScript์์ ์ ๋ ฌ์ ๋ชจ๋ ์ ์ฌ๋ ฅ์ ํ์ฉํ ์ ์์ต๋๋ค.
์ฃผ์ ์์ :
- ๊ธฐ๋ณธ sort method๋ ์์๋ฅผ ๋ฌธ์์ด๋ก ์ ๋ ฌํ๋ฏ๋ก, ์์น ์ ๋ ฌ์ด ์์์น ๋ชปํ ๊ฒฐ๊ณผ๋ฅผ ์ด๋ํ ์ ์์ต๋๋ค.
- ๋น๊ต ํจ์๋ฅผ ์ ๊ณตํ๋ ๊ฒ์ ์ซ์์ ๊ฐ์ฒด์ ์ ํํ ์ ๋ ฌ์ ์ํด ํ์์ ์ ๋๋ค.
- ๋์๋ฌธ์ ๊ตฌ๋ถ์ ์ํ๋ฒณ ์ ๋ ฌ์ ์ํฅ์ ๋ฏธ์น์ง๋ง, ์ ์ ํ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ด๋ฆฌํ ์ ์์ต๋๋ค.
- ๋ฉ์๋ ์ฒด์ด๋๊ณผ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ๊ณผ ๊ฐ์ ๊ณ ๊ธ ์ ๋ ฌ ๊ธฐ๋ฒ์ ์ ๋ ฌ ๊ธฐ๋ฅ์ ํฅ์์ํต๋๋ค.
<!-- ์ด ๊ธฐ์ฌ๋ AI์ ์ํด ์์ฑ๋์์ต๋๋ค. -->
**Note:** ์ด ๊ธฐ์ฌ๋ AI์ ์ํด ์์ฑ๋์์ต๋๋ค.