Skip to content
Discussion options

You must be logged in to vote

A common approach when using Room is to create multiple DAO queries for different sorting options.

For example, you could define queries like these in the DAO:

@query("SELECT * FROM note_table ORDER BY date DESC")
fun getNotesNewestFirst(): LiveData<List>

@query("SELECT * FROM note_table ORDER BY date ASC")
fun getNotesOldestFirst(): LiveData<List>

@query("SELECT * FROM note_table ORDER BY title ASC")
fun getNotesAlphabetically(): LiveData<List>

Then the ViewModel can expose different functions to the UI depending on the sorting option selected by the user.

Another option is to use a dropdown or menu in the UI so users can dynamically switch between sorting methods.

Handling sorting direc…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by kikulabsofficial
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants