added sort of the pattern array