How can i sort albums from songs?

前端 未结 5 577
一生所求
一生所求 2021-01-27 17:28

So my problem is that when i try to sort the albums, the album title and album art are wrong.

I tried sorting the album ids but that doesn\'t fix it because album id hav

5条回答
  •  -上瘾入骨i
    2021-01-27 17:47

    Without changing much in your code, you can achieve what you intend by sorting the Songs using a Comparator. Comparator can be used whenever you need to sort the objects using one of its properties (which is album name in your case for the QuerySongs object)

    In your getAlbumsId method, add following

    public ArrayList getAlbumsId() {
    
        ArrayList albumids = new ArrayList();
        Collections.sort(songs, new Comparator() {
            @Override
            public int compare(QuerySongs o1, QuerySongs o2) {
                return o1.getAlbum().compareTo(o2.getAlbum());
            }
        });
    
        for (QuerySongs song : songs){
            Long albumid = song.getAlbumID();
    
            if (! albumids.contains(albumid)) {
                albumids.add(albumid);
            }
        }
    
    
        return albumids;
    }
    

    Above will mutate the songs object, if you don't want that to happend, make a copy of it. Crux is to use comparator to sort the songs.

提交回复
热议问题