Fix bug with undefined sort checking