Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Stable Sort an Array with ES2019 JavaScript

    Mike SherovMike Sherov
    javascriptJavaScript

    Before ES2019, array sorting in Javascript was not guaranteed to be stable. In this lesson, we'll learn what stable sorting is by emulating stable sorting in an older version of Node. Then, we'll learn how and when stable sorting matters. Finally, we'll use nvm to switch between an older version of Node and a newer version and compare how each version deals with sorting.

    You can find installation instructions for nvm here. Stable sorting was adding to v8 v7.0 (Chrome 70 and Node 11), as noted in this blog post

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: Here we have our Nuxt.js application. It's printing out student averages. What's cool about Nuxt.js is that it's an isomorphic framework. That is, it will render new components server-side. Then once the page loads, it will render them client-side, again using the same JavaScript.

    However, this introduces an interesting wrinkle as the browser may be a different version of JavaScript, in this case Chrome, from the version of JavaScript the server is running. In this case, we are currently running Node 8.9.3.

    In our application, that has an interesting effect. If I reload the page, you'll see the names are sorted differently, Fernie appearing first. Once the JavaScript fully loads and runs client-side, Alicia will appear first.

    The reason this happens is that old versions of Node didn't have stable sorting. Stable sorting means that given two elements that are being sorted with equal sort values, the item that comes first in the input array should match the order of the item that comes in the output array.

    In this case, if Alicia and Bobby and all these students have the same averages and we're sorting by average, Alicia should come before Bobby in the output array, because she's first in the input array.

    To see how we can mimic stable sorting in older versions of Node, let's see how index.vue passes the scores.json data to scores.vue. As we can see here inside our asyncData function, we are reading scores.json, parsing it, and returning it to the asyncData function, which then gets used to render in our template.

    In this case the scores property is bound to the scores property of the input and gets passed to the scores component. The scores component will look through each record in its sorted property and print out record.student, record.average, record.grade, and record.teachers, where a record is an element in the sorted array.

    Sorted is a computed property. The sorted computed property is built by mapping the current records and transforming them and then sorting them, taking the difference of the average of the two students.

    In this case, because all students have the same average, we've hit the stable sorting problem. In order to fix this in older versions of Node, we could iterate over the records before sorting them and add a index property to the record and, when sorting them, first detect if A.average = B.average. Then return A.index - B.index and let the index be the tiebreaker.

    If we refresh our code again, we'll see that there's now no flash. Alicia appears before Bobby. Thankfully, later versions of Node solve this by having sort be stable right out of the box.

    In order to see this, I'm now going to upgrade Node. To do so, I use a program called NVM. NVM is a Node Version Manager which you can get from Homebrew or any other way to install software. You type something like NVM use 11 to install and use the latest version of Node, Node 11.

    If I type node --version now, it'll say version 11.14. If I go back to my Nuxt development server and I recompile and wait for it to build, I can now remove all that additional code I just added in. Wait for it to compile. Refresh the page. If I refresh again, I now see it prints out Alicia right away.