[Coding Test] Javascript nested sort 작성일 2021-03-06 | In Coding-test 1234567891011121314151617const ASCENDING = 1 const DESCENDING = -1 const dir = DESCENDING const priority = ['a','b'] const sortFunc = (a,b) => { for (let i=0; i<priority.length; i++){ if (a[priority[i]] > b[priority[i]]) return dir else if (a[priority[i]] < b[priority[i]]) return -dir } return 0 } const arr = [{a: 3, b: 4}, {a: 1, b: 2}, {a: 3, b: 2}] arr.sort(sortFunc) console.log(arr) dir과 priority만 바꿔서 쓰면된다.