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)
          i = bisect.bisect_right(closest_keys, key)
          closest_keys.insert(i, key)
          closest_cdx.insert(i, cdx)
          if len(closest_cdx) == limit:
              if key > closest_keys[-1]:
                  break
          if len(closest_cdx) > limit:
              closest_cdx.pop()
      for cdx in closest_cdx:
          yield cdx