Database query in the same list order

What I’m trying to do:
Query the database using unique ids(barcodes) and as result: search iterator in the same order like my list used for query. Then pass it to the repeating panel of data grid.
I can’t query the database and get the search iterator in the same order like my list used for search back.

list_barcodes = ['Br0004', 'Br0002', 'Br4000'...]
database = app_tables.sample_database.search(q.fetch_only("barcode", "study_id","name","status","matrix","ext_barcode",
                                                            "volume","order_number","comment",updated_by=q.fetch_only("email")),barcode=q.any_of(*list_barcodes))

What I’ve tried and what’s not working:

  • Instead of q.any_of(*list_barcodes) looping through the list_barcodes with a search row by row + appending a new list of rows.
    Increases time required and the result is a list of rows. That cause slow loading of data in the data grid. rows appear row by row when you switch pages.
  • order_by work on some column but in order to use it, it would be neccessary to add some numbers first, that can be used to sort it + problem with multiple users doing the same thing.

Is there some efficient way to get the search iterator for my data grid in the same order like the list used for search?

what is working but the result is slow list of single rows:

    for x in list_barcodes:
        try:
            row = app_tables.sample_database.get(ext_barcode=x)
            if row is None: 
                missing.append(x)
            else:
                barcode = row['ext_barcode']
                if barcode in list_of_barcodes:
                    duplicates.append(barcode)  # Append duplicates
                else:
                    database.append(row)  # Add the row to the search iterator
                    list_of_barcodes.append(barcode)
        except anvil.tables.TableError:
            multiple_rows.append(x)