Munkith Abid
4 min readNov 20, 2021

--

A deeper look into Javascript sort() function

There is no question that sorting in data structure and algorithm is on of most widely used and implemented techniques and it has some interested and various ways on how it’s implemented and utilized depending on several factors such as the type of structure that contains the data, the size of these structures and what type of data it represents. Sorting algorithm being that important, is almost always have been represented in one way or another in any programming language you can think of.

Today where going to talk about how the sort() function in Javascript (and some other programming languages) is meant to be used and how it behaves differently based on certain criteria and what kind of parameters we pass to it.

In Javascript, sort() function comes as part of the Array API. and is considered to be one of the most versatile functions in the entire Javascript built-in library. It shifts its behavior and outcome based on the slightest difference in the way we call it. It is also worth mentioning that we have to be extremely careful when using sort() on one of the arrays in your script as it does the sorting in place and thus changing the array it’s called on instead of returning a new sorted one. It is also NOT a pure function for this reason.

The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

Specific to Javascript, sort() function will always treat the elements in the array as strings unless it’s told otherwise, even if they were actually stored in the array as some other primitive datatype. Let’s take a look at an example:

sort() converting elements into a string and sorting them accordingly.

As long as we keep this little specification in our minds when dealing with Javascript’s sort(), we should be safe to proceed and how to sort the previous array appropriately as a collection of integers which is illustrated in the code snippet below:

Forcing sort() to treat the array elements as integers.

How this work is very basic, yet very powerful. Of course, if we wanted to sort the array in decedent order all we have to do is switch the a and b terms around the minus operator. In a nutshell, all sort() is asking for to sort things out in different manners is a call function that returns either 1; when sorting in one direction is prefered,-1; when sorting in other direction is what we want, or 0 when everything should stay in place and no sorting is required.

With this in mind, we basically can have the array sorted in pretty much various ways other than the boring descending and ascending sort of things as we will see shortly.

A while ago I came across the grouping anagrams challenge. Where we’re given an array of strings and asked to group similar words in their own array. Similar here means that each word or string contains the same characters with the exact number of occurrences. This also of course implies that for the two words to be anagrammatic, they have to be of the same length.

Now let’s translate these requirements to something sort() can understand; We basically need to write a function that maps these similarity conditions to their ternary digital counterparts.

For any given two words:

  • If the words are similar, return 1.
  • If the words are not similar then return -1.
  • If the words are identical then return 0. (This step is complementary and can be eliminated and we can consider similar and identical both as one condition).

And here is how the function looks like along with a test sample run at the bottom.

similar() implemented and passed to sort() as a sorting criteria

Here you have it, a nice and clean way to approach this problem. The similar() function is pretty straightforward and just tweaked so that it returns what sort() can understand instead of returning a boolean value.

This short article is made to shed some light on how powerful sort() is, and how can we free its potential from the regular ascending vs. descending mindset. If you are interested in learning what else this little fella can do, please refer to the resources section.

--

--