restore and refactor tags and categories sorting
add a test to ensure tags and categories are sorted