오름차순 정렬
const arrayToSort = [
['ft301b_ds', 'checked'],
['do200b_ds', 'checked'],
['err_a', 'checked'],
['ft2_ds', 'checked'],
['ft301_ds', 'checked'],
['err_b', 'checked'],
['ft102_ds', 'checked'],
['ft202_ds', 'checked'],
['srt_b', 'checked'],
];
// 첫 번째 요소([0])를 기준으로 정렬
arrayToSort.sort((a, b) => a[0].localeCompare(b[0]));
console.log(arrayToSort);
a는 자기자신 b는 비교할 다음 요소
내림차순 정렬
내림차순 정렬은 a랑 b의 위치만 바꿔주면된
const arrayToSort = [
['ft301b_ds', 'checked'],
['do200b_ds', 'checked'],
['err_a', 'checked'],
['ft2_ds', 'checked'],
['ft301_ds', 'checked'],
];
// 첫 번째 요소를 기준 내림차순정렬
arrayToSort.sort((a, b) => b[0].localeCompare(a[0]));
console.log(arrayToSort);
반응형