Improved sorting to handle empty values and numbers