Search code examples
androidandroid-roomandroid-architecture-componentsandroid-jetpack

Android Room Database: How to query alphabetically by name?


So I am using room database to persist users locally, now users have this entity class:

@Entity
public class Users{

public string name;
public string email;
@PrimaryKey
public string id;

......

}

I retrieve all these users with a DAO that looks like this:

@DAO
public interface UsersDAO{

@Query("select * from Users")
public List<Users> getAllUsers();


}

The above DAO will read all users in the order they were added to the database.

Question:

Is it possible to query users by the name field in Users entity alphabetically?

Thanks.


Solution

  • Use the ORDER BY keyword.

    "select * from Users ORDER BY name"