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.