Notice
Recent Posts
Recent Comments
Link
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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 27 28 29 30
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[JavaScript] lodash sortBy ๋ณธ๋ฌธ

๐Ÿง‘‍๐Ÿ’ป Web/JavaScript

[JavaScript] lodash sortBy

Lennon 2022. 7. 2. 00:02
728x90
๋ฐ˜์‘ํ˜•

react, javascript์—์„œ๋Š” ๋ณดํ†ต sort๋ฅผ ์ด์šฉํ•ด ์ •๋ ฌ์„ ๋งŽ์ดํ•œ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ sort๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋‘๊ฐ€ ์•Œ๋‹ค์‹œํ”ผ ์ฝ”๋“œ ๊ฐ€๋…์„ฑ์ด ๋งŽ์ด ๋–จ์–ด์งˆ ์ˆ˜ ๋ฐ–์— ์—†๋‹ค.
๋˜ํ•œ sort๋Š” ์ด์ƒํ•œ ๋ฌธ์ œ์ ์ด ์žˆ๋‹ค.

const arr = [1,3,20,5];

const sortedArr = arr.sort();

console.log(sortedArr) // [1,20,3,5]

sort๋ฅผ ๊ทธ๋ƒฅ ์‚ฌ์šฉํ•˜๋ฉด ์œ„์ฒ˜๋Ÿผ ์šฐ๋ฆฌ๊ฐ€ ์˜ˆ์ƒํ•œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค...(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ... ใ… .ใ… ๐Ÿฅฒ)

 

๊ทธ๋กœ์ธํ•ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐœ๋ฐœ์ž๋ผ๋ฉด

๋ณดํ†ต ๊ธฐ๋ณธ sort๋ฅผ ๋ฐ”๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  (a,b) ๋‘ ์ธ์ž๋ฅผ ๋น„๊ตํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด sort๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

const arr = [1,3,20,5];

const sortedArr = arr.sort((a,b) => a - b);

console.log(sortedArr) // [1,3,5,20]

 

๊ธฐ๋ณธ์ ์ธ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด sort๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ์–ด๋–ค์ง€ ํ™•์ธํ•ด๋ณด์ž!

const userData = [
	{ id: 5, name: 'kenny', birth: 19970529},
	{ id: 2, name: 'mile', birth: 19960122},
	{ id: 2, name: 'toil', birth: 19920912},
	{ id: 4, name: 'jack', birth: 20010621},
	{ id: 7, name: 'hendy', birth: 20071121},
]

์œ„์™€ ๊ฐ™์ด ์„œ๋ฒ„์—์„œ objectํ˜•ํƒœ๋กœ ๋‚ ๋ผ์˜จ userData๊ฐ€ ์กด์žฌํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž.

 

๋จผ์ € id ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด๋ณด์ž!

function App() {
  const userData = [
    { id: 5, name: "kenny", birth: 19970529 },
    { id: 2, name: "mile", birth: 19960122 },
    { id: 2, name: "amily", birth: 19920912 },
    { id: 4, name: "jack", birth: 20010621 },
    { id: 7, name: "hendy", birth: 20071121 },
  ];

  const sortedUserId = userData.sort((a, b) => a.id - b.id);

  console.log(sortedUserId);

  return <></>;
}

์—ฌ๊ธฐ๊นŒ์ง„ ๊ดœ์ฐฎ์€ ๊ฒƒ ๊ฐ™๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ id๊ฐ€ ๊ฐ™์„ ๋•Œ name์„ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋กœ ๋‹ค์‹œ ์ •๋ ฌํ•˜๊ณ  ์‹ถ๋‹ค.

function App() {
  const userData = [
    { id: 5, name: "kenny", birth: 19970529 },
    { id: 2, name: "mile", birth: 19960122 },
    { id: 2, name: "mile", birth: 19920912 },
    { id: 4, name: "jack", birth: 20010621 },
    { id: 7, name: "hendy", birth: 20071121 },
  ];

  const sortedUserId = userData.sort((a, b) => {
    if (a.id === b.id) {
      if (a < b) return -1;
      if (a > b) return 1;
      if (a === b) return 0;
      else return -1;
    }
    return a.id - b.id;
  });

  console.log(sortedUserId);

  return <></>;
}

๋ญ”๊ฐ€ ๋” ๋ณต์žกํ•ด์กŒ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋ฆ„๋„ ๊ฐ™๋‹ค๋ฉด ๋‚˜์ด๊ฐ€ ๋งŽ์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์–ด์กŒ๋‹ค.

function App() {
  const userData = [
    { id: 5, name: "kenny", birth: 19970529 },
    { id: 2, name: "mile", birth: 19960122 },
    { id: 2, name: "mile", birth: 19920912 },
    { id: 4, name: "jack", birth: 20010621 },
    { id: 7, name: "hendy", birth: 20071121 },
  ];

  const sortedUserId = userData.sort((a, b) => {
    if (a.id === b.id) {
      if (a < b) return -1;
      if (a > b) return 1;
      if (a === b) {
        return a.birth - b.birth;
      } else return -1;
    }
    return a.id - b.id;
  });

  console.log(sortedUserId);

  return <></>;
}

๋ญ”๊ฐ€ ์‚ฌ์‹ค ๊ทธ๋ƒฅ ์ •๋ ฌ๋งŒ ํ•˜๊ณ  ์‹ถ์—ˆ์„ ๋ฟ์ธ๋ฐ ์ฝ”๋“œ๊ฐ€ ๋งŽ์ด ๋‚œ์žกํ•ด์กŒ๋‹ค. ์ด๋ฅผ ๋” ๋ฉ‹์ง€๊ฒŒ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์—†์„๊นŒ?

sortBy() 

๊ทธ๋ž˜์„œ sortBy()๊ฐ€ ์กด์žฌํ•œ๋‹ค!

lodash์˜ sortby๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

lodash ์„ค์น˜ ๋ฐฉ๋ฒ•

$ npm i -g npm
$ npm i --save lodash

 

 

๋ฆฌ์•กํŠธ๋ฅผ ์˜ˆ์‹œ๋กœ ์œ„ ๋งˆ์ง€๋ง‰ ์ฝ”๋“œ๋ฅผ sortBy๋ฅผ ์จ์„œ ํ‘œ์‹œํ•ด๋ณด๋ฉด

import sortBy from "lodash/sortBy";

function App() {
  const userData = [
    { id: 5, name: "kenny", birth: 19970529 },
    { id: 2, name: "mile", birth: 19960122 },
    { id: 2, name: "mile", birth: 19920912 },
    { id: 4, name: "jack", birth: 20010621 },
    { id: 7, name: "hendy", birth: 20071121 },
  ];

  const sortedUserId = sortBy(userData, "id", "name", "birth");

  console.log(sortedUserId);

  return <></>;
}

export default App;

์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค...!

reverse()๋ฅผ ์จ์„œ ์—ญ์ˆœ ์ •๋ ฌ๋„ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์›ํ•˜๋Š” ๋Œ€๋กœ ์ด์–ด๊ฐ€๋ฉฐ ์ •๋ ฌ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

import sortBy from "lodash/sortBy";

function App() {
  const userData = [
    { id: 5, name: "kenny", birth: 19970529 },
    { id: 2, name: "mile", birth: 19960122 },
    { id: 2, name: "mile", birth: 19920912 },
    { id: 4, name: "jack", birth: 20010621 },
    { id: 7, name: "hendy", birth: 20071121 },
  ];

  const sortedUserId = sortBy(userData, "id", "name", "birth").reverse();

  console.log(sortedUserId);

  return <></>;
}

export default App;

sortBy๋ฅผ ์•„๋Š” ๊ฐœ๋ฐœ์ž๋ผ๋ฉด ์œ„ ์ฝ”๋“œ๊ฐ€ ๋” ๋งค๋ ฅ์ ์ธ ์ฝ”๋“œ๋กœ ๋ณด์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

ํ”„๋ก ํŠธ๋‹จ์—์„œ object ํ˜•์‹์˜ ์„œ๋ฒ„ ๋ฐ์ดํ„ฐ๋ฅผ props๋กœ ๋ฐ›์•„ ๋„์šธ ๋•Œ sortBy๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.  

728x90
๋ฐ˜์‘ํ˜•
Comments