generateHashFormula now sorts the newFormula before returning it