# How to set default descending sorting order in a tree view?

**URL:** https://discuss.tryton.org/t/how-to-set-default-descending-sorting-order-in-a-tree-view/3739
**Category:** Developer
**Created:** [January 29, 2021, 4:33pm UTC](https://discuss.tryton.org/t/how-to-set-default-descending-sorting-order-in-a-tree-view/3739 "2021-01-29T16:33:47Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![banknote](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/banknote/32/980_2.png) [@banknote](https://discuss.tryton.org/u/banknote)
#### Post date: [January 29, 2021, 4:33pm UTC](https://discuss.tryton.org/t/how-to-set-default-descending-sorting-order-in-a-tree-view/3739/1 "2021-01-29T16:33:47Z")

</div>

I am trying to set the sort order of a tree view.  
Now I have the oldest record first:

 ![image](https://discuss-cdn.tryton.org/uploads/default/original/2X/f/f4d66fc33c8e4103deef4dcbb3e54cf8f6e82eee.png)

And the following XML:

```auto
<tree string="Inspections" sequence="id" tree_state="1">
    <field name="inspection_datetime" select="1" widget="date"/>
    <field name="inspection_datetime" width="50" select="2" widget="time"/>
    <!-- SKIPPED -->
    <field name="id" tree_invisible="1"/>
</tree>

```

I have no idea — how to set a descending order of ids???

Constantine.

---

<div class="post-metadata">

### Author: ![sergyo](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/sergyo/32/181_2.png) [@sergyo](https://discuss.tryton.org/u/sergyo)
#### Post date: [January 29, 2021, 4:56pm UTC](https://discuss.tryton.org/t/how-to-set-default-descending-sorting-order-in-a-tree-view/3739/2 "2021-01-29T16:56:45Z")

</div>

> [@banknote](#):
>
> how to set a descending order of ids???

ordering is defined either in [python side (for default value)](https://hg.tryton.org/modules/sale/file/tip/sale.py#l271) or [in the action](https://hg.tryton.org/modules/stock_lot/file/tip/stock.xml#l104).

---

<div class="post-metadata">

### Author: ![banknote](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/banknote/32/980_2.png) [@banknote](https://discuss.tryton.org/u/banknote)
#### Post date: [January 29, 2021, 4:58pm UTC](https://discuss.tryton.org/t/how-to-set-default-descending-sorting-order-in-a-tree-view/3739/3 "2021-01-29T16:58:01Z")

</div>

Well, that’s pitty, but ok.  
And thank you so much!  
It works perfectly now.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [January 29, 2021, 5:29pm UTC](https://discuss.tryton.org/t/how-to-set-default-descending-sorting-order-in-a-tree-view/3739/4 "2021-01-29T17:29:29Z")

</div>

You can also click on the column header to sort on its value (multiple times to toggle the direction).

---

<div class="post-metadata">

### Author: ![banknote](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/banknote/32/980_2.png) [@banknote](https://discuss.tryton.org/u/banknote)
#### Post date: [February 2, 2021, 6:37am UTC](https://discuss.tryton.org/t/how-to-set-default-descending-sorting-order-in-a-tree-view/3739/5 "2021-02-02T06:37:46Z")

</div>

That is exactly the thing my customer is complaining about: “Why on Earth I should click three times on the header to see the most recent record on top?!” 🙂

---

<div class="post-metadata">

### Author: ![nicoe](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/nicoe/32/2880_2.png) [@nicoe](https://discuss.tryton.org/u/nicoe)
#### Post date: [February 2, 2021, 3:20pm UTC](https://discuss.tryton.org/t/how-to-set-default-descending-sorting-order-in-a-tree-view/3739/6 "2021-02-02T15:20:17Z")

</div>

It’s something that they should do only if the default do not suit them in one rare occurence.  
Otherwise as @sergyo said you can also adapt the default to their liking.

---

<div class="post-metadata">

### Author: ![banknote](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/banknote/32/980_2.png) [@banknote](https://discuss.tryton.org/u/banknote)
#### Post date: [February 2, 2021, 3:25pm UTC](https://discuss.tryton.org/t/how-to-set-default-descending-sorting-order-in-a-tree-view/3739/7 "2021-02-02T15:25:53Z")

</div>

Yes, my customer always need the most recent record on top, that is not the default behavior.  
I did exactly as it was proposed by @sergyo! And I like it. I didn’t understand that way from the docs. He pointing me to the example and — that was what I need.  
I ended with the following solution:

```auto
        <!-- # Open window with Inspections -->
        <record model="ir.action.act_window" id="act_inspections">
                <field name="name">Inspections</field>
                <field name="order"
                eval="[('id', 'DESC'), ('assess_datetime', 'DESC')]"
                pyson="1"/>                
                <field name="res_model">mymodule.assessment</field>
        </record>

```

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [February 2, 2021, 4:06pm UTC](https://discuss.tryton.org/t/how-to-set-default-descending-sorting-order-in-a-tree-view/3739/8 "2021-02-02T16:06:56Z")

</div>

Setting an default order on `ModelSQL._order` would remove the need to define it on all actions and also applies to view without action.

> [@banknote](#):
>
> `eval="[('id', 'DESC'), ('assess_datetime', 'DESC')]"`

You do not need the second clause because ‘id’ is unique so it define by itself the full order.

---

<div class="post-metadata">

### Author: ![banknote](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/banknote/32/980_2.png) [@banknote](https://discuss.tryton.org/u/banknote)
#### Post date: [February 2, 2021, 4:11pm UTC](https://discuss.tryton.org/t/how-to-set-default-descending-sorting-order-in-a-tree-view/3739/9 "2021-02-02T16:11:55Z")

</div>

Yes, I know about `id`, I just left both for having the ability to switch between these two ways to which it would be better for the customer. And we finally agreed on the id version, so `assess_datetime` is excessive and should be removed.

Thank you, I will try `ModelSQL._order` with another view…

---

<div class="post-metadata">

### Author: ![banknote](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/banknote/32/980_2.png) [@banknote](https://discuss.tryton.org/u/banknote)
#### Post date: [February 20, 2021, 6:23pm UTC](https://discuss.tryton.org/t/how-to-set-default-descending-sorting-order-in-a-tree-view/3739/10 "2021-02-20T18:23:59Z")

</div>

I used the following declaration:

```auto
class SaleAnalytics(ModelSQL, ModelView):
    # description (mandatory on first declaration)
    'Sale Analytics'

    # Internal class name. Always used as a reference inside Tryton
    # default: '<module_name>.<class_name>' on Tryton
    # becomes '<module_name>_<class_name>' in the database
    __name__ = 'mymodule.analytics.sales'

    _order_name = 'report_date'
    _order = [('report_date', 'DESC')]

```

I shared it as it seems to work fine. For those who follow my path…

---

<div class="post-metadata">

### Author: ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)
#### Post date: [March 22, 2021, 6:24pm UTC](https://discuss.tryton.org/t/how-to-set-default-descending-sorting-order-in-a-tree-view/3739/11 "2021-03-22T18:24:06Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
