如何在房间数据库中进行排序

huangapple go评论65阅读模式
英文:

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 = &quot;movies&quot;)
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(&quot;SELECT * FROM movies&quot;)
    LiveData&lt;List&lt;Movie&gt;&gt; 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(&quot;SELECT * FROM movies ORDER BY voteAverage&quot;)

you can use ASC or DESC for ascending and descending order

huangapple
  • 本文由 发表于 2020年7月26日 01:42:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63091545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定