def _drop_nans_and_sort(df, group_cols, subgroup_col):
    """Only keep entries that have valid values in the important columns and sort.

    We sort them first by their group_cols and then by the subgroup_col.
    The later insures that bricks of the same subgroup column are stacked together.
    Within group stacks bricks are ordered by their value.
    """
    drop_and_sort_cols = group_cols.copy()
    if subgroup_col is not None:
        drop_and_sort_cols.append(subgroup_col)
    drop_and_sort_cols += ["value", "id"]
    df = df.dropna(subset=drop_and_sort_cols, how="any")
    df = df.sort_values(drop_and_sort_cols)
    return df