'Sort a list of left-joins by their interdependency
@param joins: the list of joins'

@classmethod
def sort(cls, joins):

    if (len(joins) <= 1):
        return joins
    r = list(joins)
    tables = current.db._adapter.tables
    append = r.append
    head = None
    for i in xrange(len(joins)):
        join = r.pop(0)
        head = join
        tablenames = tables(join.second)
        for j in r:
            try:
                tn = j.first._tablename
            except AttributeError:
                tn = str(j.first)
            if (tn in tablenames):
                head = None
                break
        if (head is not None):
            break
        else:
            append(join)
    if (head is not None):
        return ([head] + cls.sort(r))
    else:
        raise RuntimeError('circular    join    dependency')
