I recommend avoiding this situation if you can, e.g. by:
- Setting a 'write-a-function' question instead of a 'write-a-program' question. For example, ask the student to write a function that returns a list of numbers. The test code can then call the function and do its own checking, e.g. sorting the student's answer before comparing with the expected.
- Tightening the specification to require a specific ordering of the output.
Alternatively, you may also be able to use a regular expression grader instead of exact match. For the simple example you quote, the expected output might be set to something like
\A(1 2)|(2 1)\s*\Z. But regular expression graders confuse students so this isn't usually a good approach.
If you really must ask a question in which the output needs to be passed through some custom validator, you'll need to use a template grader. See https://github.com/trampgeek/moodle-qtype_coderunner#grading-with-templates. However, this is a big step up in complexity.