小编Pac*_*hun的帖子

Collection.sort /反向/ reverOrder

我有一个歌曲的Arraylist存储标题,艺术家和时间我写了我的所有代码和一切.我只是想了解更多关于Collection.sort和Collection.reverse以及Collection.reverseOrder的信息.我有一个包含所有歌曲和所有内容的文件.我想根据时间按降序排序歌曲.

当我尝试这个时,我得到一个错误或它没有正确排序.任何人都可以建议我如何使用Collection.sort并使用比较器

Comparator<Song> comparator = Collections.reverseOrder();
Collections.reverse(listOfSongs);
Run Code Online (Sandbox Code Playgroud)

我的比较方法如下:

public int compare(Song mySong1,Song mySong2 ){
        if (mySong1.getLength() > mySong2.getLength()){
            return -1; 
        }
        if(mySong1.getLength() < mySong2.getLength()){
            return 1;
        }
        if(mySong1.getLength() == mySong2.getLength())
        {
            if(mySong1.getTitle().compareTo(mySong2.getTitle()) > 0){
                return -1;
            }
            if(mySong1.getTitle().compareTo(mySong2.getTitle())  < 0 ){
                return 1;
            }
            else {
                if(mySong1.getComposer().compareTo(mySong2.getComposer())  >0){
                    return -1;
                }
                if(mySong1.getComposer().compareTo(mySong2.getComposer())  <0) {
                    return 1;
                }
            }
        }
        return 0; 
    }
Run Code Online (Sandbox Code Playgroud)

java sorting algorithm collections

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

algorithm ×1

collections ×1

java ×1

sorting ×1