First we need to prepare the dict, then sort and format it:
text = """#ifndef bb2421a299d24d4abbe59c3721a794f9
#define bb2421a299d24d4abbe59c3721a794f9
enum {{
{}
}};
#endif//bb2421a299d24d4abbe59c3721a794f9
""".format(",\n".join(map(lambda x: "{0[0]} = {0[1]}".format(x), sorted(kernel_ids.items(), key=lambda x: x[1]))))
Functions used: sorted, map, format.
Notes: