import pandas as pd


def order_cluster(cluster_field_name, target_field_name, df, ascending):
    df_new = df.groupby(cluster_field_name)[target_field_name].mean().reset_index()
    df_new = df_new.sort_values(by=target_field_name, ascending=ascending).reset_index(
        drop=True
    )
    df_new["index"] = df_new.index
    df_final = pd.merge(
        df, df_new[[cluster_field_name, "index"]], on=cluster_field_name
    )
    df_final = df_final.drop([cluster_field_name], axis=1)
    df_final = df_final.rename(columns={"index": cluster_field_name})
    return df_final
