Fix column sort algorithm, add unit test