Added 'sort' method to swap branches based on a given condition