code: def arrangeByType(service_list, preferred_types):
      
  
      def enumerate(elts):
          """Return an iterable that pairs the index of an element with
          that element.
  
          For Python 2.2 compatibility"""
          return zip(range(len(elts)), elts)
  
      def bestMatchingService(service):
          """Return the index of the first matching type, or something
          higher if no type matches.
  
          This provides an ordering in which service elements that
          contain a type that comes earlier in the preferred types list
          come before service elements that come later. If a service
          element has more than one type, the most preferred one wins.
          """
          for i, t in enumerate(preferred_types):
              if preferred_types[i] in service.type_uris:
                  return i
  
          return len(preferred_types)
  
      # Build a list with the service elements in tuples whose
      # comparison will prefer the one with the best matching service
      prio_services = [(bestMatchingService(s), orig_index, s)
                       for (orig_index, s) in enumerate(service_list)]
      prio_services.sort()
  
      # Now that the services are sorted by priority, remove the sort
      # keys from the list.
      for i in range(len(prio_services)):
          prio_services[i] = prio_services[i][2]
  
      return prio_services
docs: Rearrange service_list in a new list so services are ordered by
      types listed in preferred_types.  Return the new list.