fixed sort by checking for null or undefined