General rules-based sorting of lists