Fixed the bug causing sorting to fail
The wrong array was being referenced, meaning that the unsorted values
were always being used, even after sorting.

In a previous attempt to fix the bug, added a pair of functions to push
all the null values to the end of the array.
These will be removed in the next commit.