Sort directory contents in test before assertion

Should fix recent test failures, but I can't replicate failure on my
computer, so it's not clear that it's fixed.
Merge pull request #6419 from takluyver/sort-test-dir-contents

Sort directory contents in test before assertion