[SOLVED] Sorting data table by date, unexpected result

Hello everyone, first question in the forum so thanks for the help!

I have a repeating panel that is bound to a data table. The app data table has a date column. In my repeating panel, I have added a row header with a link to sort the column by date and am getting a weird result. When I sort the table by date, it doesn’t sort in ascending order backwards like you would expect:

It seems like the dates sort going forward in time for a specific year, then sort going backwards in time by the year. This leads me to believe the year is being sorted as the primary sort, and then month is being sorted next, and perhaps day after that. Here is my sorting code on the table that allows for switching the sorting method to ascend forward or backward:

def link_9_click(self, **event_args):
if self.sort_flag == 0:
self.repeating_panel_1.items = self.samples.search(tables.order_by(‘date’, ascending = False))
self.sort_flag = 1
else:
self.repeating_panel_1.items = self.samples.search(tables.order_by(‘date’, ascending = True))
self.sort_flag = 0

What I am looking to do is sort in a true fashion by date going through time rather than the weird sorting that is currently happening. Thanks for any suggestions!

Ethan

Hmm, it seems to sort properly for me. Here is a simple clone to demonstrate.

https://anvil.works/build#clone:TXWJOQY5CPCATUAY=KIDVWK3X62TRLHJ2JWZVBYDU

Is your datatable column set to “date” types (not text)?


Note that you can also share clones which can help others to debug (if you don’t mind sharing your app). Click the :gear: and follow the instructions.

Also you can format your code nicely on the forum by wrapping in backticks with the word python.

For example,

```python

print('this will be syntax highlighted')

```

Well I figured it out, I had made a stupid error. My data table has a ‘date_added’ column and also a ‘sample_date’ column, and I was just sorting by the wrong column. Thanks so much for the help and suggestions on code formatting and app cloning @alcampopiano, will be sure to use that in future questions