added check to avoid unnecessary sorting