Fix #14: Optimize sorting of atoms in 'generate_file' function