code: def cdx_sort_closest(closest, cdx_iter, limit=10):
      
      closest_cdx = []
      closest_keys = []
      closest_sec = timestamp_to_sec(closest)
  
      for cdx in cdx_iter:
          sec = timestamp_to_sec(cdx[TIMESTAMP])
          key = abs(closest_sec - sec)
  
          # create tuple to sort by key
          #bisect.insort(closest_cdx, (key, cdx))
  
          i = bisect.bisect_right(closest_keys, key)
          closest_keys.insert(i, key)
          closest_cdx.insert(i, cdx)
  
          if len(closest_cdx) == limit:
              # assuming cdx in ascending order and keys have started increasing
              if key > closest_keys[-1]:
                  break
  
          if len(closest_cdx) > limit:
              closest_cdx.pop()
  
      for cdx in closest_cdx:
          yield cdx
docs: sort CDXCaptureResult by closest to timestamp.