sort by multiple keys
let votes = [
{ title: 'Apple', votes: 1 },
{ title: 'Milk', votes: 2 },
{ title: 'Carrot', votes: 3 },
{ title: 'Banana', votes: 2 }
];
// โญ๏ธ ไธ่กๆๅฎ๏ผ็ๆฏๅคฉๆโ๏ธ
let sorted = votes.sort((a,b) =>
// 1. ๅ
ๆ votes (ๅค็ไบบๅ
)
// 2. ๅๆ title (ๆๅญๆฏ้ ๅบ)
b.votes - a.votes || a.title.localeCompare(b.title)
);
/*
[
{ title: 'Carrot', votes: 3 },
{ title: 'Banana', votes: 2 },
{ title: 'Milk' , votes: 2 },
{ title: 'Apple' , votes: 1 }
]
*/
ๅ็ญๅนณๅๅๅไธๅ - ๆๅไธๅๆๆ็จๅฐใ
Last updated