def sort_by_hierarchy(tids, taxdump):
    """Sort a sequence of taxIds by hierarchy from low to high.

    Parameters
    ----------
    tids : list of str
        taxIds to sort
    taxdump : dict
        taxonomy database

    Returns
    -------
    list of str
        sorted taxIds
    """
    # start with any taxId from pool
    seq = [tids[0]]
    pool = [x for x in tids[1:]]

    # loop until pool is drained
    while pool:
        found = False
        for i, tid in enumerate(pool):

            # add to end of sequence
            if taxdump[seq[-1]]['parent'] == tid:
                seq.append(tid)
                found = True

            # add to beginning of sequence
            elif taxdump[tid]['parent'] == seq[0]:
                seq.insert(0, tid)
                found = True

            # remove from pool
            if found:
                pool.pop(i)
                break

        # if none can be added, then they are not sortable (i.e., not a
        # sequence in the taxonomic hierarchy)
        if not found:
            raise ValueError('Cannot sort taxIds by hierarchy.')
    return seq