May I shyly point you at this?
It is possible though that Smart Rules with their on-board features will do everything you need already. The advantage of a script like that is that all the sorting is a) done in one place, b) in exact the order you want, and c) because of the latter allows a fall-back destination for everything that has not a specific destination.