Because we've already gone behind the scenes of sort(), I shall assume you know how it works, no? Why not! You also know that left on its own, sort() would order things in somewhat unpredictable but alphabetically-seeming order. For example,

    var randomMonths = ['Feb', 'Nov', 'Mar', 'Jan', 'Jun', 'Dec', 'Apr', 'Jul', 'Aug', 'Sep', 'May', 'Oct']

    randomMonths.sort() // ["Apr", "Aug", "Dec", "Feb", "Jan", "Jul", "Jun", "Mar", "May", "Nov", "Oct", "Sep"]

Ordered like that isn't the months in the year anyone is familiar with, except we're in an elementary class learning A-B-C-D. Are we?

But knowing that a sorter() function allows us to define how we want sort() to compare the values each pair of element in order to return -1, 1, 0, which return value determines where it places an item in the array, means we can actually alter the values of these element pair by getting their indexOf() from another array - our preferably ordered one. JavaScript would then use that order to sort for us. Hopefully, JavaScript doesn't cheat!

It's all a mouthful that we've said up there. Let's illustrate.

By the way, because in this "custom sorter" function, we're going to use a tweak to the original sorter() signature, I should repeat it here for reference:

    var sorter = (a, b) => a < b ? -1 : 1

Let's get back to our custom months sorter, which this time, we shall call it sortBy, as in sort the given array by get the idea.

        months = ['Feb', 'Nov', 'Mar', 'Jan', 'Jun', 'Dec', 'Apr', 'Jul', 'Aug', 'Sep', 'May', 'Oct'],
        order = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

    function sortBy(arr, order) {
        arr.sort((a, b) => order.indexOf(a) < order.indexOf(b) ? -1 : 1)
        return arr

    sortBy(months, order) // ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

Yeah, so we got what we wanted, but why does a part of me feel like JavaScript cheated 😄? Like it just looked at our ideal order array and thought, well, this is what they want, uhn, just spit it back to them. Or, maybe not?

Yeah, maybe not. See, JavaScript on the first call compares "Feb" and "Nov". But their indices this time aren't respectively 0 and 1 but 1 and 10 because coming from our order array, get that? So, "Feb" is made to precede "Nov" because 1 < 10.

Then "Nov" and "Mar" are compared with indices coming from order - respectively 10 and 2. 10 > 2 so "Nov" gets pushed in front. At this stage, I imagine that JavaScript also just does another comparing with "Feb" and "Mar" in order to know that the latter has to come in front.

So, you get the idea...this looping and comparing goes on until sortBy has ran out of the a, b comparison pair. After which, you get an ordered months array from a random one.

Our sortBy function will come in handy pretty much often as you work more with arrays in more practical, real-world scenarios. Do save it in your convenient helper library.