오브젝트의 특정 프로퍼티로 정렬하기

설명
const sortBy = (arr, key) => arr.sort((a, b) => a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : 0);
Tags
util function
 
const sortBy = (arr, key) => arr.sort((a, b) => a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : 0);
 
const lessons = [{ position: 1, name: "Intro" }, { position: 0, name: "Basics" }]; sortBy(lessons, 'position'); // {position: 0, name: 'Basics'}, // {position: 1, name: 'Intro'}