sort the defs and refs for stability