Onyx Database is capable of sorting and paging.
This tutorial demonstrates how to order your results as well as limit the number of results and the starting index of your search.
As an example, we will retrieve a list of all "Player" entities in the database and specifically we will order the results by lastName and firstName.
We will retrieve the first page of results then the second.
To see how the data was seeded for this example, see Main.java.
Get an Instance of the PersistenceManager.
Create a List of QueryOrders
A QueryOrder can be used to sort the result set. There is not limit to the number of QueryOrders you can use in a single Query.
This list of QueryOrders below will retrieve a list of players and order them by lastName ascending, then firstName ascending.
The QueryOrder constructor supports 2 arguments:
attribute The attribute name you are sorting by.
ascending Boolean flag for sort directions. When false the results will be returned in descending order.
Create a Query with QueryOrder
This example uses one of the many constructors that are available.
Set the firstRow and maxResults to Implement Paging and Retrieve the First Page
The firstRow is zero-index, so if you would like the first page set it to 0
Query for the First Page (rows 0-9, records 1-10)
Print the First Page Results
The output looks like this :
Set the firstRow and maxResults to Retrieve the Second Page
Query for the Second Page (rows 10-19, records 11-20)