reworked sorting function
Sorting Hat complete