import asyncio


def get_running_tasks(cmd_name) -> list[asyncio.Task] | None:
    """Returns the list of tasks for a given command name, sorted by start date."""

    all_tasks = [(t, getattr(t, "_command_name", None)) for t in asyncio.all_tasks()]

    # Sort by date but exclude potential tasks without our added _date
    matching = [t[0] for t in all_tasks if t[1] == cmd_name]
    matching_dated = [(m, getattr(m, "_date")) for m in matching if hasattr(m, "_date")]

    if len(matching) == 0:
        return None

    return [m[0] for m in sorted(matching_dated, key=lambda x: x[1])]