Jamund Ferguson: 0:00 Our API allows us to return a list of notes. We'd like to be able to return that list in ascending or descending order based on the query parameter that gets sent in. We've updated our route to take in a query parameter here, but we needed to tell our model as well.
0:15 Start by sending sort to the getNotes() method on Note. Then let's go into the model and apply a sorting function. Start by storing the results from NOTES.values in a variable called notes. We'll set up the return statement.
0:27 Between these, type notes.sort. We'll pass in a function, this time an arrow function, that takes two parameters. We'll just call them a and b. Here, we'll check which sort we have. If we're doing ascending sort, then we'll return a.lastEdited - b.lastEdited. In the else, in the descending case, we'll return b.lastEdited - a.lastEdited.
1:06 One thing we also need to do is make sure the lastEdited is set every time we change our note, so lastEdited was set in createNote. We also need to make sure that we set that in our updateNote() method. note.lastEdited = Date.now(). We don't need to set it for get or delete.
1:24 We can save the file, start the server. We come here and add two notes, hello world and we'll call the second one second world. Let's see how the sorting works. First of all, we'll try to sort it in descending order. We should expect to see first the newest one, second. Indeed, you see second and then hello, if you look at the order in the returned array.
1:48 Let's sort in ascending, then hello should be first, hello, and then second.
1:52 Let's remove any kind of sort parameter, and you will see it goes back to descending order, second and then hello.