Create order function for Function field with some left joins

Hi, I want to create a order function for a function field in
my supplier invoices list and I need to do some left joins to do the relations between tables.
This funcion doesn’t order my list correctly and I think that the relations are well done. I suspect that is for the sintaxis. This is my code: .

    @staticmethod
    def order_purchases_list(tables):
        pool = Pool()
        Purchase = pool.get('purchase.purchase')
        PurchaseLine = pool.get('purchase.line')
        InvoiceLine = pool.get('account.invoice.line')
        account_invoice, _ = tables[None]

        table, _ = tables[None]
        invoiceline_tables = tables.get('invoice-line')
        if invoiceline_tables is None:
            invoice_line = InvoiceLine.__table__()
            invoiceline_tables = {None: (invoice_line, invoice_line.invoice ==
                                         account_invoice.id), }
            tables['invoiceline'] = invoiceline_tables

        purchaseline_tables = tables.get('purchase-line')
        if purchaseline_tables is None:
            purchase_line = PurchaseLine.__table__()
            purchaseline_tables = {None: (
                purchase_line, purchase_line.id ==
                Cast(Substring(invoice_line.origin,
                     Position(',', invoice_line.origin) + Literal(1)),
                     PurchaseLine.id.sql_type().base)), }
            tables['purchaseline'] = purchaseline_tables

        purchase_tables = tables.get('purchase')
        if purchase_tables is None:
            purchase = Purchase.__table__()
            purchase_tables = {None: (purchase, purchase.id ==
                                      purchase_line.purchase), }
            tables['purchase'] = purchase_tables

        return [purchase.number]

Someone sees something wrong?

The order method must return a SQL expression to be used in the ORDER BY part of the query.

sorry edited the post, it was not completed

The purchase number is a Char so it does not order as human could expect. For that you should re-use the expression from Purchase.order_number.

I tried to return this:
[CharLength(purchase.number), purchase.number]
But still not working, I don’t know if you mean that.

What do you mean by not working?

I mean that my order_ function is not ordering how it should.

Why can you say that? Please provide evidence.

This is how is ordering now:

For me you joins are wrong because as far as I see the field compute a comma separated list of number but the order result is just one number.
I think it is very complicated to write such order method because you need to build a SQL query that compute the comma separated list. Because you can not define a joins that is a cartesian product (so not like One2Many but only with Many2One).

Okayy, thanks a lot!