Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 645
data Avatar asked Aug 25 '18 14:08

data


1 Answers

Use the ORDER BY keyword.

"select * from Users ORDER BY name"
like image 121
Bertram Gilfoyle Avatar answered Oct 05 '22 02:10

Bertram Gilfoyle