correctly write out sorted tags