๊ฐ๋ฐ์ ํ๋ค ๋ณด๋ฉด ๋ฐ์ดํฐ ์ ๋ ฌ์ ํผํ ์ ์๋ ์ผ์์ด ๋ฉ๋๋ค. ํ๋ก์ ํธ๋ ์ฝ๋ฉ ํ ์คํธ๋ฅผ ๋ง๋ก ํ๊ณ ๋ฐฐ์ด์ด๋ ๊ฐ์ฒด๋ฅผ ์ ๋ฆฌํ๋ ์์ ์ ์ฌ์ฌ์ฐฎ๊ฒ ๋ง์ฃผ์น๊ฒ ๋์ฃ . ์ค๋์ ์ด๋ฐ ์ํฉ์์ ์ ์ฉํ๊ฒ ์จ๋จน์ ์ ์๋ ๋ค์ํ ์ ๋ ฌ ๊ธฐ๋ฒ๋ค์ ํจ๊ป ์์๋ณด๋ ค๊ณ ํฉ๋๋ค.
๋ฐฐ์ด ์ ๋ ฌํ๊ธฐ
1. ๊ธฐ๋ณธ ์ ๋ ฌ (sort ๋ฉ์๋)
๋ฐฐ์ด์ sort() ๋ฉ์๋๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ์ ๋ ฌ ๋ฐฉ๋ฒ์ผ๋ก, ์ ๋ ฌ ์์๋ ๋ฌธ์์ด์ ์ ๋์ฝ๋ ์ฝ๋ ํฌ์ธํธ๋ฅผ ๋ฐ๋ฆ ๋๋ค.
const fruits = ['banana', 'apple', 'orange', 'grape'];
fruits.sort();
console.log(fruits); // ['apple', 'banana', 'grape', 'orange']
โ ๏ธ sort() ๋ฉ์๋๋ ๊ธฐ๋ณธ์ ์ผ๋ก(๋น๊ต ํจ์๋ฅผ ์ ๊ณตํ์ง ์์ ๊ฒฝ์ฐ) ์์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ์ฌ ์ ๋ ฌํฉ๋๋ค.
โ ๏ธ ์ ๋ ฌ๋ ๋ณต์ฌ๋ณธ์ ๋ฐํํ๋ ๊ฒ์ด ์๋๋ผ, ์ ๋ฐฐ์ด์ด ์ ๋ ฌ๋ฉ๋๋ค.
2. ์ซ์ ๋ฐฐ์ด ์ ๋ ฌ
์ซ์ ๋ฐฐ์ด์ ์ ๋ ฌํ ๋๋ ๋น๊ต ํจ์(compareFunction)๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
numbers.sort((a, b) => a - b); // ์ค๋ฆ์ฐจ์
console.log(numbers); // [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
numbers.sort((a, b) => b - a); // ๋ด๋ฆผ์ฐจ์
console.log(numbers); // [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]
3. 2์ฐจ์ ๋ฐฐ์ด ์ ๋ ฌํ๊ธฐ
let numMatrix = [
[3, 2, 1],
[1, 1, 5],
[2, 3, 4],
[2, 1, 3]
];
// ์ฒซ ๋ฒ์งธ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ๊ณ ,
// ๊ฐ์ผ๋ฉด ๋ ๋ฒ์งธ ์์๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
numMatrix.sort((a, b) => {
if (a[0] !== b[0]) {
return a[0] - b[0];
}
return a[1] - b[1];
});
console.log(numMatrix);
// [
// [1, 1, 5],
// [2, 1, 3],
// [2, 3, 4],
// [3, 2, 1]
// ]
4. ์ ๋ ฌ๋ ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ก ๋ฐ๊ธฐ
// ๋ฌธ์์ด
const strings = ['b', 'c', 'd', 'a']
const sorted = Array.from(strings.keys())
.sort((a, b) => strings[a].localeCompare(strings[b]))
console.log(sorted) // [3, 0, 1, 2]
// ์ซ์
const numbers = [3, 1, 2, 4]
let sorted = Array.from(numbers.keys())
.sort((a, b) => numbers[b] - numbers[a]);
console.log(sorted) // [3, 0, 2, 1]
๊ฐ์ฒด ์ ๋ ฌํ๊ธฐ
1. ํค๋ก ์ ๋ ฌํ๊ธฐ
const obj = { b: 2, a: 1, c: 3 };
const sortedKeys = Object.keys(obj).sort();
const sortedObj = {};
sortedKeys.forEach(key => {
sortedObj[key] = obj[key];
});
console.log(sortedObj); // { a: 1, b: 2, c: 3 }
2. ๊ฐ์ผ๋ก ์ ๋ ฌํ๊ธฐ
const obj = { b: 2, a: 1, c: 3 };
const sortedEntries = Object.entries(obj).sort((a, b) => a[1] - b[1]);
const sortedObj = Object.fromEntries(sortedEntries);
console.log(sortedObj); // { a: 1, b: 2, c: 3 }
ํ์ฉ ์์
1. ์ฌ๋ฌ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ธฐ
const students = [
{ name: 'Alice', grade: 'A', age: 22 },
{ name: 'Bob', grade: 'B', age: 20 },
{ name: 'Charlie', grade: 'A', age: 21 }
];
students.sort((a, b) => {
if (a.grade !== b.grade) {
return a.grade.localeCompare(b.grade);
}
return a.age - b.age;
});
console.log(students);
// [
// { name: 'Alice', grade: 'A', age: 22 },
// { name: 'Charlie', grade: 'A', age: 21 },
// { name: 'Bob', grade: 'B', age: 20 }
// ]
2. ๋์๋ฌธ์ ๊ตฌ๋ถ ์์ด ์ ๋ ฌํ๊ธฐ
const names = ['alice', 'Bob', 'charlie', 'David'];
names.sort((a, b) => a.localeCompare(b, undefined, { sensitivity: 'base' }));
console.log(names); // ['alice', 'Bob', 'charlie', 'David']
3. ์ฌ์ฉ์ ์ ์ ์์๋ก ์ ๋ ฌํ๊ธฐ
const fruits = ['banana', 'apple', 'orange', 'grape'];
const order = ['orange', 'banana', 'apple', 'grape'];
fruits.sort((a, b) => order.indexOf(a) - order.indexOf(b));
console.log(fruits); // ['orange', 'banana', 'apple', 'grape']
4. ์์ (stable)ํ ์ ๋ ฌํ๊ธฐ
์๋ฐ์คํฌ๋ฆฝํธ์ sort() ๋ฉ์๋๋ ES2019๋ถํฐ ์์ ์ ๋ ฌ์ ๋ณด์ฅํฉ๋๋ค. ์ด์ ๋ฒ์ ์์ ์์ ์ ๋ ฌ์ ๊ตฌํํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ ์ ์์ต๋๋ค:
let data = [
{ id: 1, value: "b" },
{ id: 2, value: "a" },
{ id: 3, value: "b" },
{ id: 4, value: "a" }
];
data
.map((item, index) => ({ item, index }))
.sort((a, b) => {
// value๋ก ๋จผ์ ์ ๋ ฌํ๊ณ , ๊ฐ์ผ๋ฉด ์๋ ์ธ๋ฑ์ค๋ก ์ ๋ ฌ
return a.item.value.localeCompare(b.item.value) || a.index - b.index;
})
.map(({ item }) => item);
console.log(data);
// [
// { id: 2, value: "a" },
// { id: 4, value: "a" },
// { id: 1, value: "b" },
// { id: 3, value: "b" }
// ]
Reference
'๐ฅ Develop > Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Javascript] ์ ๊ทํํ์ (1) | 2024.10.19 |
---|---|
[Javascript] Object.entries() ๋ฉ์๋ (0) | 2024.07.27 |
[Javascript] ์คํ๋ ๋(Spread) ์ฐ์ฐ์ (0) | 2024.07.26 |
[Javascript] ๋ฐ์ดํฐ ํ์ ์ ๋ฉ๋ชจ๋ฆฌ ์ฐธ์กฐ ๋ฐฉ์ (1) | 2024.07.24 |
[Javascript] ํด๋ก์ (Closure) (3) | 2024.07.20 |