System Design Quiz Practice
Active Recall Drill Session
← Exit SessionQuestion 1 of 1
hardSystem Design
Given this paginateAndSort function, what does calling it with sortKey 'age' and sortDirection 'desc' on this data produce for page 1, pageSize 2?
code
function paginateAndSort(items, { page, pageSize, sortKey, sortDirection }) {
const sorted = sortKey
? [...items].sort((a, b) => {
const cmp = a[sortKey] < b[sortKey] ? -1 : a[sortKey] > b[sortKey] ? 1 : 0;
return sortDirection === "desc" ? -cmp : cmp;
})
: items;
const start = (page - 1) * pageSize;
return { items: sorted.slice(start, start + pageSize) };
}
const data = [{ name: "C", age: 25 }, { name: "A", age: 30 }, { name: "B", age: 20 }, { name: "D", age: 35 }];
console.log(paginateAndSort(data, { page: 1, pageSize: 2, sortKey: "age", sortDirection: "desc" }).items.map(i => i.name));