Array sorting is easy, but can yield unexpected results if you are not careful.
Consider this example -
|
|
All beautiful and compliant so far.
But, what if Loki lurks his head?
|
|
This problem occurs all the time since you are receiving arrays from external systems, from other libraries, or from code written by your “best friends” (quote intended).
Arrays can now turn up results like this..
|
|
Best way to avoid surprises? Filter.
|
|
I am just returning the truthy value of val
to ensure we filter our null, undefined etc.
Your mileage may vary and you may have to change tactics depending on the type of array you get and how you process it after sort.