add shared logic for tag sorting

To merge with [HabitRPG/#3865](https://github.com/HabitRPG/habitrpg/pull/3865)