def sort_fields(elem, field_orders):
 order_dicts = dict()
 for (field, order) in field_orders.items():
  order_dicts[field] = order_key = dict()
  for (i, subfield) in enumerate(order):
   order_key[subfield] = i
 _sort_fields(elem, order_dicts)

def sort_fields ( elem , field_orders ) : order_dicts = dict ( ) for ( field , order ) in field_orders . items ( ) : order_dicts [ field ] = order_key = dict ( ) for ( i , subfield ) in enumerate ( order ) : order_key [ subfield ] = i _sort_fields ( elem , order_dicts )
sort the elements and subelements in order specified in field_orders .