๋ฐ˜์‘ํ˜•

 

๊ฐœ๋ฐœ์„ ํ•˜๋‹ค ๋ณด๋ฉด ๋ฐ์ดํ„ฐ ์ •๋ ฌ์€ ํ”ผํ•  ์ˆ˜ ์—†๋Š” ์ผ์ƒ์ด ๋ฉ๋‹ˆ๋‹ค. ํ”„๋กœ์ ํŠธ๋‚˜ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋ฅผ ๋ง‰๋ก ํ•˜๊ณ  ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด๋ฅผ ์ •๋ฆฌํ•˜๋Š” ์ž‘์—…์€ ์‹ฌ์‹ฌ์ฐฎ๊ฒŒ ๋งˆ์ฃผ์น˜๊ฒŒ ๋˜์ฃ . ์˜ค๋Š˜์€ ์ด๋Ÿฐ ์ƒํ™ฉ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์จ๋จน์„ ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์ •๋ ฌ ๊ธฐ๋ฒ•๋“ค์„ ํ•จ๊ป˜ ์•Œ์•„๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

๋ฐฐ์—ด ์ •๋ ฌํ•˜๊ธฐ

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

Javascript: Sort array and return an array of indices

Sorting object property by values

๋ฐ˜์‘ํ˜•
์œค๋„๊ธฐ