code: def _sorted_by_priority(self, fmtos, changed=None):
          def pop_fmto(key):
              idx = fmtos_keys.index(key)
              del fmtos_keys[idx]
              return fmtos.pop(idx)
          def get_children(fmto, parents_keys):
              all_fmtos = fmtos_keys + parents_keys
              for key in fmto.children + fmto.dependencies:
                  if key not in fmtos_keys:
                      continue
                  child_fmto = pop_fmto(key)
                  for childs_child in get_children(
                          child_fmto, parents_keys + [child_fmto.key]):
                      yield childs_child
                  if (any(key in all_fmtos for key in child_fmto.parents) or
                          fmto.key in child_fmto.parents):
                      continue
                  yield child_fmto
          fmtos.sort(key=lambda fmto: fmto.priority, reverse=True)
          fmtos_keys = [fmto.key for fmto in fmtos]
          self._last_update = changed or fmtos_keys[:]
          self.logger.debug("Update the formatoptions %s", fmtos_keys)
          while fmtos:
              del fmtos_keys[0]
              fmto = fmtos.pop(0)
              for child_fmto in get_children(fmto, [fmto.key]):
                  yield child_fmto
              if any(key in fmtos_keys for key in fmto.parents):
                  continue
              yield fmto