fixes #20, sorting files before writing