Added general function term sorting. Methods such as differentiate require sorting to function properly.