after many many attempts to sort the entries, I think this works, even if the code is terrible