We have an API that allows us to return a list of notes at /notes
. We want to add the ability to sort that list by passing in the sort
query param, which we read off of req.query
. Once we know the expected sort order we pass a function to Array.prototype.sort
that compares two notes' lastEdited
values to decide which item comes first.
A quick refresher about how custom Array sort functions work in JavaScript. Sort takes a function with two params (which are items in the array). If the function returns true, the a
item will be placed higher in the sorted array. If the function returns false
, the b
item will be placed higher.
View the source for this project on the projects github repo.