Fix invalid markup after sorting