const students = [
{
id: "1",
name: "Sherlock",
score:90
},
{
id: "2",
name: "Genta",
score: 75
},
{
id: "3",
name: "Ai",
score: 80
},
{
id: "4",
name: "Budi",
score:85
}
]
and result
{ id: '1', name: 'Sherlock', score: 90 },
{ id: '4', name: 'Budi', score: 85 }
You will need to filter the students by their score first, and then sort them by their score (and name if scores are the same).
const filterStudentsByMinScore = (students, minScore) =>
students
.filter(({ score }) => score >= minScore)
.sort(({ name: na, score: sa }, { name: nb, score: sb }) =>
sb - sa || na.localeCompare(nb));
const students = [
{ id: "1", name: "Sherlock" , score: 90 },
{ id: "2", name: "Genta" , score: 75 },
{ id: "3", name: "Ai" , score: 80 },
{ id: "4", name: "Budi" , score: 85 },
];
const results = filterStudentsByMinScore(students, 85);
console.log(results);
.as-console-wrapper { top: 0; max-height: 100% !important; }