Added a null check to sorting algorithm.