def METHOD_NAME(
    X: npt.NDArray[np.float32],
    dim: Optional[int] = None,
    max_items: Optional[int] = None,
    flatten: bool = True,
) -> Union[List[int], List[List[int]]]:
    """
    Performs a multi-objective sort by iteratively computing the Pareto front
    and sparsifying the items within the Pareto front. This is a non-dominated
    sort leveraging an epsilon-net. All metrics are normalized using quantile
    normalization.
    Args:
        X: Array of shape [N, D] (N: number of items, D: dimensionality) with the
            multi-dimensional items to sort.
        dim: The feature (metric) to prefer when ranking items within the Pareto front. If `None`,
            items are chosen randomly.
        max_items: The maximum number of items that should be returned. When this is `None`, all
            items are sorted.
        flatten: Whether to flatten the resulting array.
    Returns:
        The indices of the sorted items, either globally or within each of the Pareto fronts,
        depending on the value of `flatten`.
    """
    remaining = np.arange(X.shape[0])
    indices = []
    num_items = 0
    # First, we want to normalize the input by performing a quantile transform into the range
    # [0, 1]
    X = (st.rankdata(X, axis=0) - 1) / (X.shape[0] - 1)  # type: ignore
    # Iterate until max_items are reached or there are no items left
    while remaining.size > 0 and (max_items is None or num_items < max_items):
        # Compute the Pareto front and sort the items within
        pareto_mask = pareto_efficiency_mask(X[remaining])
        pareto_front = remaining[pareto_mask]
        pareto_order = epsilon_net_indices(X[pareto_front], dim=dim)
        # Add order to the indices
        indices.append(pareto_front[pareto_order].tolist())
        num_items += len(pareto_front)
        # Remove items in the Pareto front from the remaining items
        remaining = remaining[~pareto_mask]
    # Restrict the number of items returned and optionally flatten
    if max_items is not None:
        limit = max_items - sum(len(x) for x in indices[:-1])
        indices[-1] = indices[-1][:limit]
        if not indices[-1]:
            indices = indices[:-1]
    if flatten:
        return [i for ix in indices for i in ix]
    return indices