What is the best way to implement note sorting in this Room-based notes app? #13
-
|
Hi everyone 👋 I’ve been studying this NoteUsingRoom project to learn more about how Room Database works in Android applications. One thing I’m curious about is how we could implement sorting for the notes list. For example, it might be useful for users to sort notes by:
What would be the recommended approach to implement sorting using Room queries? Would it be better to create multiple DAO queries or handle sorting on the UI side? I’d really appreciate any suggestions or best practices. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
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") @query("SELECT * FROM note_table ORDER BY date ASC") @query("SELECT * FROM note_table ORDER BY title ASC") 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 directly in Room queries is usually better than sorting in the UI because the database can do it more efficiently. |
Beta Was this translation helpful? Give feedback.
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…