code: def _slice_mostly_sorted(array, keep, rest, ind=None):
      if ind is None:
          ind = np.arange(len(array))
      idx = np.argsort(np.concatenate([keep, ind[rest]]))
      slices = []
      if keep[0] > 0:
          slices.append(slice(None, keep[0]))
      slices.append([keep[0]])
      windows = zip(keep[:-1], keep[1:])
      for l, r in windows:
          if r > l + 1:
              slices.append(slice(l + 1, r))
          slices.append([r])
      if keep[-1] < len(array) - 1:
          slices.append(slice(keep[-1] + 1, None))
      result = da.concatenate([array[idx[slice_]] for slice_ in slices])
      return result