英文:
How to sort in room database
问题
嘿。
如何按评分(voteAverage)对电影列表进行排序?
@Entity(tableName = "movies")
public class Movie {
@PrimaryKey(autoGenerate = true)
private int uniqueId;
private int id;
private String genre;
private int voteCount;
private String title;
private String originalTitle;
private String overview;
private String posterPath;
private String bigPosterPath;
private String backdropPath;
private double voteAverage;
...
MovieDao
@Dao
public interface MovieDao {
@Query("SELECT * FROM movies")
LiveData<List<Movie>> getAllMovies();
...
由于某种原因,它不会按照 @Query("SELECT * FROM movies SORT BY voteAverage") 进行排序
英文:
Hey.
How can I sort the list of movies by rating?(voteAverage)
@Entity(tableName = "movies")
public class Movie {
@PrimaryKey(autoGenerate = true)
private int uniqueId;
private int id;
private String genre;
private int voteCount;
private String title;
private String originalTitle;
private String overview;
private String posterPath;
private String bigPosterPath;
private String backdropPath;
private double voteAverage;
...
MovieDao
@Dao
public interface MovieDao {
@Query("SELECT * FROM movies")
LiveData<List<Movie>> getAllMovies();
...
For some reason, it doesn't sort by @Query("SELECT * FROM movies SORT BY voteAverage")
答案1
得分: 5
使用 ORDER BY
@Query("SELECT * FROM movies ORDER BY voteAverage")
你可以使用 ASC
或者 DESC
来进行升序和降序排列
英文:
Use ORDER BY
@Query("SELECT * FROM movies ORDER BY voteAverage")
you can use ASC
or DESC
for ascending and descending order
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论