def sortFitness(chromosomes, target):
    """Sort the chromosomes so the fitest comes first."""
    done = False
    fittest = []
    for chrom in chromosomes:
        # Decode
        pheno = decode(chrom)

        # Try to evaluate
        try:
            result = eval(pheno)
        except ZeroDivisionError:
            result = 0

        # Score based on result
        fit = abs(target-result)
        if fit == 0:
            done = True
            fit = -1
        print "%s =%s : %s" %(pheno.rjust(15), str(result).rjust(10),
                                      str(fit).rjust(15))
        fittest.append((fit, chrom))
    # Once we have a list of (fitness, chrom) pairs we can sort
    fittest.sort()
    return done, [item[-1] for item in fittest]
Sort the chromosomes so the fitest comes first.
recipes/Python/498257_Simple_functibase_Genetic/recipe-498257.py