Added for loop in sortNames function to sort names alphabetically