'Custom orderby logic for datatables
@ToDo: Support for self.translate = True
need to handle the inevitable NULL values which vary in
order by DB, although perhaps DB handling doesn\'t matter
here.'

def dt_orderby(self, field, direction, orderby, left):

    otable = current.s3db.org_organisation
    left.add(otable.on((field == otable.id)))
    if self.parent:
        rotable = otable.with_alias('org_root_organisation')
        left.add(rotable.on((otable.root_organisation == rotable.id)))
        orderby.extend([('org_root_organisation.name%s' % direction), ('org_organisation.name%s' % direction)])
    else:
        orderby.append(('org_organisation.name%s' % direction))
