code: def sort_by_list_order(sortlist, reflist, reverse=False, fltr=False,
                         slemap=None):
      
  
      def keyfunc(entry):
          if slemap is not None:
              rle = slemap(entry)
          if rle in reflist:
              # Ordering index taken from reflist
              return reflist.index(rle)
          else:
              # Ordering index taken from sortlist, offset
              # by the length of reflist so that entries
              # that are not in reflist retain their order
              # in sortlist
              return sortlist.index(entry) + len(reflist)
  
      if fltr:
          if slemap:
              sortlist = filter(lambda x: slemap(x) in reflist, sortlist)
          else:
              sortlist = filter(lambda x: x in reflist, sortlist)
  
      return sorted(sortlist, key=keyfunc, reverse=reverse)
docs: Sort a list according to the order of entries in a reference list.
  
      Parameters
      ----------
      sortlist : list
        List to be sorted
      reflist : list
        Reference list defining sorting order
      reverse : bool, optional (default False)
        Flag indicating whether to sort in reverse order
      fltr : bool, optional (default False)
        Flag indicating whether to filter `sortlist` to remove any entries
        that are not in `reflist`
      slemap : function or None, optional (default None)
         Function mapping a sortlist entry to the form of an entry in
         `reflist`
  
      Returns
      -------
      sortedlist : list
        Sorted (and possibly filtered) version of sortlist