Sort and Return an Array of Objects with a Query Param in an Express API

Jamund Ferguson
InstructorJamund Ferguson
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

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.

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.

[0:50] A quick refresher on how sort method works on arrays in JavaScript. You pass in two values from the array. If you return a negative number, the a value will return lower than the b value in the sorted array. If you return a positive number, the a value will return higher.

[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 = 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.

~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today