Javascript .sort Method

Sorting Out the Confusion

Travis Prol
Sep 8, 2020 · 3 min read

The Javascript .sort method is one that I have struggled with for quite some time. I always found myself looking it up and fighting with it until one day I decided to save it in a file somewhere, and copy and paste it into my project whenever I needed to sort an array. This is clearly not the best way to code because you do not truly know how to use all of your tools.

Below we have an array of objects where each object holds a person’s first name, last name, and age. We can use the sort method to sort these people by any of these fields.

The .sort function allows the passing in of a callback function, which then accepts two parameters. These two parameters are two of the elements from the array.

When .sort is called, it cycles through the array and returns a negative number, a positive number, or 0. These returns are based on which element (a or b) you want to appear before the other one. Let’s take a look at these returns:

If you are trying to sort by a field that is a number, this function will work by subtracting b.age from a.age. Make sure to return it!

This function properly sorts that array by everyone’s age!

Let’s break down what the sort function did to the original array. Since Travis was first in the array, he becomes the a parameter, and since Christiana was second, she becomes the b parameter. When Christiana’s age of 22 is subtracted from Travis’ age of 24, 2 is returned. Since 2 is greater than 0, the sort function changes the order of the array, and puts the b element before the a. The function then reassigns what a and b are. Since Travis was moved to the second spot, he is now a, and Zach who is next in the array is now b. This continues until the entire array as been sorted.

If you try to do this with any other field, however, it will not work because if you try to subtract a string from a string, it does not return a number, and the order cannot change. In order to fix this, we need to explicitly tell the function what to return.

The Javascript comparison operators let us compare strings and will return booleans based on the comparison.

We can see here in that in a Javascript console, that “apple” is less than “banana”. This is looks backwards of what you might expect it to be because B is the second letter of the alphabet (2), while A is the first (1).

Bringing this concept into our sort function, we can tell the function what value we want it to return based on the comparison of two strings.

Here, we are telling the function that if a < b, return -1 which, according to the rules of the function, will keep a first. When we return 1, b will be put first, and if they are the same, return 0, which will not change anything.

This function will sort the array correctly!

The Startup

Get smarter at building your thing. Join The Startup’s +788K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Travis Prol

Written by

Full Stack Web Developer 🤓 Lets go Yanks ⚾️

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Travis Prol

Written by

Full Stack Web Developer 🤓 Lets go Yanks ⚾️

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store