linux rearange field and sort by column
There is no reason to use an external file and command. Python can sort strings just fine, unless the file is too big

something like this should work

def rearrange_ids(file):
    pattern = re.compile(r'id(\d+)')
    for line in file:
        spt = line.strip().split()
        if spt:
            ids = sorted(int(pattern.findall(id_string)[0]) for id_string in spt[0:2])
            yield ids, '\t'.join(['id%i' % i for i in ids] + spt[2:]) + '\n'

with open(input_file, 'r') as file, open(output_filename, 'w') as output_file:
    output_lines = sorted(rearrange_ids(file), key=lambda x: x[0])
    output_file.writelines(line for ids, line in output_lines)



id1   id2 ....
id1   id2 ....
id1   id3 ....
id3   id4 ....
id3   id4 ....



edit

I changed my original algorithm because id did not keep the original order when comparing, and sorted 10 before 2