Sort keys by default for a stable order, and make sort function configurable.