Sorting results from datatable search query

hoping this isn’t a totally dumb question but i am a bit stumped.

I have a data table with 3 columns as follows:

  1. Your_Email (the email of the authenticated user)
  2. Processed_file (a media file that the user has uploaded and has been processed by the app)
  3. DateTime (the date and time the table record was created/added to the table)

When the authenticated user of the app goes to a particular form, the form (via a repeating panel) displays the authenticated user’s documents they have processed in the past. each row of the the repeating panel displays their email, the document they processed etc.

The problem is that the information is displayed in the panel starting with the oldest processed document first, down to the most recently processed document. which means the user must always go to the last document in the displayed panel list to get to the document they just processed (bad user experience). This is due to when a new row is added to the data table, it is added to the end of the table by default via the add_row() method, and when data is extracted form the table (if not sorted) it defaults to the existing order of rows in the table ie., the last row is the most recent.

I want the newest documents to appear first in the repeating panel, with the older docs following in decreasing date processed order

I assumed I could easily query/extract from the table a given particular user records based on their email (the Email table column), then sort the extracted records based on date (the DateTime column), and return that sorted object to be displayed in the repeating panel which would then be displayed in the proper most recent document first, oldest last order.

However, this scenario is not covered in the documentation as far as i can tell (Anvil Docs | Using Data Tables from Python). for example in the docs:

oldest_first = app_tables.people.search(
 tables.order_by("name"),
 tables.order_by("age", ascending=False)
)

the searching and ordering is done by the name of the columns, on all the rows. not what I want to do. I don’t want to have to sort the entire table every time a user goes to the form that displays their documents. it seems much more efficient to me to extract and sort only their specific records.

And in my case I need to first extract the particular users records based on their email as follows:

def get_my_docs():
  emailname = anvil.users.get_user()['email']
  #extract that users documents (aka the rows in the data table) by their email address
  docs = app_tables.aws_processed.search(Your_Email = emailname)
  return docs

which extracts the user records by the user email name, but the docs are ordered by oldest to newest, which is not what I want. I need to then sort those extracted records so they are ordered newest to oldest. So I need to do something like this:

def get_my_docs():
  emailname = anvil.users.get_user()['email']
  docs = app_tables.aws_processed.search(Your_Email = emailname)
  #now that I have the users (and only that users) rows, sort rows by DateTime column into ordered_docs. this is what i need to understand how to do
  
  return ordered_docs

any suggestion on how can I do that? thank you, James

Well, it may not be intuitive, but this is not the case.

This is the most efficient way to do the job:

oldest_first = app_tables.people.search( 
  tables.order_by("name"),
  tables.order_by("age", ascending=False),
  Your_Email=anvil.users.get_user()['email']
)

The search function under the hood will convert all of that into sql code and ask the database to take care of the sorting and searching. The database engine is many gazillion times faster than any python code you can come up with, so if there is a way to get the job done by one database call rather than by your python logic, that’s always the way to go.

The database engine will use the available indexes and figure out which rows to read, and get them directly in the correct order, without even sorting them. I think this is not the case here, but in some cases it’s so good that it will get you what you need without even getting the rows (assuming that the indexes and the queries are built accordingly).

3 Likes

Great response, and blazing fast (like the database engine :slight_smile: ).

Thank you Stefano…I will implement as you indicate.

James

2 Likes