added sorting to detection