'Return a list of 2-tuples of form (expr, (sql, params, is_ref)) for the
ORDER BY clause.
The order_by clause can alter the select clause (for example it
can add aliases to clauses that do not yet have one, or it can
add totally new select clauses).'

def get_order_by(self):

    if self.query.extra_order_by:
        ordering = self.query.extra_order_by
    elif (not self.query.default_ordering):
        ordering = self.query.order_by
    else:
        ordering = (self.query.order_by or self.query.get_meta().ordering or [])
    if self.query.standard_ordering:
        (asc, desc) = ORDER_DIR['ASC']
    else:
        (asc, desc) = ORDER_DIR['DESC']
    order_by = []
    for field in ordering:
        if hasattr(field, 'resolve_expression'):
            if (not isinstance(field, OrderBy)):
                field = field.asc()
            if (not self.query.standard_ordering):
                field.reverse_ordering()
            order_by.append((field, False))
            continue
        if (field == '?'):
            order_by.append((OrderBy(Random()), False))
            continue
        (col, order) = get_order_dir(field, asc)
        descending = (True if (order == 'DESC') else False)
        if (col in self.query.annotation_select):
            order_by.append((OrderBy(Ref(col, self.query.annotation_select[col]), descending=descending), True))
            continue
        if (col in self.query.annotations):
            order_by.append((OrderBy(self.query.annotations[col], descending=descending), False))
            continue
        if ('.' in field):
            (table, col) = col.split('.', 1)
            order_by.append((OrderBy(RawSQL(('%s.%s' % (self.quote_name_unless_alias(table), col)), []), descending=descending), False))
            continue
        if ((not self.query._extra) or (col not in self.query._extra)):
            order_by.extend(self.find_ordering_name(field, self.query.get_meta(), default_order=asc))
        elif (col not in self.query.extra_select):
            order_by.append((OrderBy(RawSQL(*self.query.extra[col]), descending=descending), False))
        else:
            order_by.append((OrderBy(Ref(col, RawSQL(*self.query.extra[col])), descending=descending), True))
    result = []
    seen = set()
    for (expr, is_ref) in order_by:
        if self.query.combinator:
            src = expr.get_source_expressions()[0]
            for (idx, (sel_expr, _, col_alias)) in enumerate(self.select):
                if (is_ref and (col_alias == src.refs)):
                    src = src.source
                elif col_alias:
                    continue
                if (src == sel_expr):
                    expr.set_source_expressions([RawSQL(('%d' % (idx + 1)), ())])
                    break
            else:
                raise DatabaseError('ORDER    BY    term    does    not    match    any    column    in    the    result    set.')
        resolved = expr.resolve_expression(self.query, allow_joins=True, reuse=None)
        (sql, params) = self.compile(resolved)
        without_ordering = self.ordering_parts.search(sql).group(1)
        if ((without_ordering, tuple(params)) in seen):
            continue
        seen.add((without_ordering, tuple(params)))
        result.append((resolved, (sql, params, is_ref)))
    return result
