DEV Community

Cover image for How not to sort an array in JavaScript

How not to sort an array in JavaScript

Phil Nash on August 25, 2019

Array sorting is one of those things you don’t spend too long thinking about, until it stops working for you. Recently I was working with array of ...
Collapse
 
isalevine profile image
Isa Levine

Hi Phil, this is a great writeup! Do you mind if I link to this at the bottom of my recent article on JavaScript sorting? I'd love to direct people to the inconsistencies and gotchas with nulls and undefineds. :)

Collapse
 
philnash profile image
Phil Nash

I would love it if you wanted to link to it! Thanks!

Collapse
 
isalevine profile image
Isa Levine

Thanks Phil! Link has been added! :)

(dev.to/isalevine/a-quick-review-of...)

Collapse
 
pheeria profile image
Olzhas Askar

I think the biggest disadvantage of sort() is that it sorts in place.
You need to do some relatively ugly hacks like spreading:

[...array].sort().map(console.log)

in order to use the power of FP.

Collapse
 
zhuangya profile image
zhuangya

it returns an array of undefineds :/

Collapse
 
damon profile image
damon

That's because the console.log returns undefined, and it's being used in the call to .map().

var array = [9, 2, 3, 8, 1, 0]
var orderedArray = [...array].sort()

console.log(array)
// [9, 2, 3, 8, 1, 0]
console.log(orderedArray)
// [0, 1, 2, 3, 8, 9]

var logReturn = console.log("hello world")
// hello world
console.log(logReturn)
// undefined
Thread Thread
 
zhuangya profile image
zhuangya

exactly, my point is we should not use map here at all.

Collapse
 
philnash profile image
Phil Nash

Yeah, it is a shame that it acts in place as we move to code that attempts to reduce the number of side effects. At least we can work around it, the problem is just remembering to.

Collapse
 
macorifice profile image
Stefano

Hi Phil,
would not have been the case to use lodash for undefined and null ?

Collapse
 
vjarysta profile image
Valentin Jarysta • Edited

This would have worked as well, therefore you might not need lodash.

Collapse
 
philnash profile image
Phil Nash

Hi Stefano, not sure what you mean there. Could you explain more?

Collapse
 
macorifice profile image
Stefano
Thread Thread
 
pavelloz profile image
Paweł Kowalski

Wow. Using lodash for that. Wow. :)

Thread Thread
 
philnash profile image
Phil Nash

Oh, sure, I could have used that. I’d probably prefer to just use an array filter though, as I do later in the post, as it will only filter the things I want and don’t incur the overhead of requiring a library like (or even just a function from) lodash.

Collapse
 
pavelloz profile image
Paweł Kowalski

Hmm, i always filter the falsy values by doing arr.filter(x => x) (i probably would do that if i wanted <1 numbers in there though ;)

Collapse
 
pheeria profile image
Olzhas Askar

I like

array.filter(Boolean)
Collapse
 
pavelloz profile image
Paweł Kowalski

Nice one, didnt know that one :)

Thread Thread
 
philnash profile image
Phil Nash

They are both cool filter expressions! 😎

But... they both lose 0 or '' which you might not want. That's why I like the undefined check.

> const array = [-1, 0, 1]
[ -1, 0, 1 ]
> array.filter(Boolean)
[ -1, 1 ]
> array.filter(x => x)
[ -1, 1 ]
Collapse
 
acostalima profile image
André Costa Lima

Very informative, thank you! 😊👌💪

Collapse
 
philnash profile image
Phil Nash

Glad you enjoyed it, thanks!