scrimba
Advanced JavaScript I
JS 89 - Exercise: Sort by field
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

JS 89 - Exercise: Sort by field
AboutCommentsNotes
JS 89 - Exercise: Sort by field
Expand for more info
index.js
run
preview
console
// https://javascript.info/closure
// Sort by field
let users = [
{ name: "John", age: 20, surname: "Johnson" },
{ name: "Pete", age: 18, surname: "Peterson" },
{ name: "Ann", age: 19, surname: "Hathaway" }
];

// by name (Ann, John, Pete)
users.sort((a, b) => a.name > b.name ? 1 : -1);

// by age (Pete, Ann, John)
users.sort((a, b) => a.age > b.age ? 1 : -1);


users.sort(byField('name'));
users.sort(byField('age'));
Console
ReferenceError: sayHi is not defined (/index.js:15)
,
index.html
-0:54