"one-step" approach - sorted items

Hello,
Let’s say I have a table of a Workplace, which holds a List of Employee. I use the “one-step” approach to fetch them. Now, If I have a particular Workplace, and I want to retrieve a sorted list (by “firstname”, for example) of it’s Employees, What is the best way doing so?

Android - Java

Thank you.

Your QueryOptions has a property called ‘sortBy’ which can be used to sort the results https://backendless.com/documentation/data/android/data_search_and_query.htm

eg options.addSortBy(“firstname desc”)

Right, but for applying the addSortBy I need to query on the Employee class. it will return ALL the Employees in the table, not just the once related to a particular Workplace.

You can then add the WhereClause to filter the employees.

dataQuery.setWhereClause(“workplace.id = 1”) or something similair

Well, I can, but this is a bit messy. if I have a huge inventory of Employees (10^6), it would be very ineffective going through all of them, just to sort a few Employees of a particular Workplace. After all, this is why I store a list of them in the Workplace table the first place, to keep it effective.

I ask if there might be a better way of doing it.

Thank you for the replay.

If you have a lot of data, try to use such workaround.
Firstly - add index for column “name” in your Employe table.
Secondary - do not create relations to Workplace (to avoid joins), just use column “WorkplaceId” and add index for this column too.

Then use dataQuery.setWhereClause() and addSortBy() as usuall.

What about the option of adding a Workspace field into the Employee class that will simply point to it’s Workspace?

Sorting by relation field is not supported. You can sort only by primitive type columns.