sort the rules before generating SQL