def rank_environment_and_phenotypes(environment, phenotypes, k=15):
    """
    Clusters sets of resources/tasks using a weighted hamming distance such
    that you can have few enough values to give each group of similar things a
    different color. This function is designed for cases when you want to
    color both an environment and a set of phenotypes such that the colors
    corespond to each other.

    Takes an EnvironmentFile object, a 2d array of phenotypes, and, optionally,
    a number indicating the maximum number of clusters (default 15).

    Returns:
     - An EnvironmentFile in which the grid has been replaced with integers
       indicating which cluster a cell is a member of. Integers are assigned
       such that cells containing more or more complex resources have higher
       numbers.

     - A 2D grid of numbers representing the clusters each phenotype was
       assigned to.

     - An integer representing the total number of clusters.
    """
    environment = convert_world_to_phenotype(environment)
    ranks = get_ranks_for_environment_and_phenotypes(environment, phenotypes)
    environment, n = assign_ranks_by_cluster(environment, k, ranks)
    phenotypes, n = assign_ranks_by_cluster(phenotypes, k, ranks)
    return environment, phenotypes, n