Move tag-sorting code to util module.