Add sorting to memory database