Implement minmax function

See #5268