๋ฐ์ํ
Intro
์๋ ํ์ธ์. ์ด๋ฒ ์๊ฐ์๋ JavaScript ์์ Object array ๋ฅผ ์ ๋ ฌํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์๋ณด๊ฒ ์ต๋๋ค.
How to do
์๋์ ์ค๋ธ์ ํธ ์ด๋ ์ด๊ฐ ์๋ค๊ณ ๊ฐ์ ํด๋ด ์๋ค.
const objectArray = [
{ name: 'aa', year: 2008 },
{ name: 'bb', year: 2004 },
{ name: 'cc', year: 2003 },
{ name: 'dd', year: 1996 },
{ name: 'ee', year: 1995 },
{ name: 'ff', year: 2015 }
];
javascript์ sort() ๋ฉ์๋๋ฅผ ํตํด์ year ํ๋ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ์ ํด๋ณด๊ฒ ์ต๋๋ค.
objectArray.sort((x, y) => x.year - y.year);
console.log(objectArray);
์ฝ๋ ์คํ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
[
{
"name": "ee",
"year": 1995
},
{
"name": "dd",
"year": 1996
},
{
"name": "cc",
"year": 2003
},
{
"name": "bb",
"year": 2004
},
{
"name": "aa",
"year": 2008
},
{
"name": "ff",
"year": 2015
}
]
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
๋ฐ์ํ