Collection.sort/reverse/reverOrder

发布于 2024-12-02 01:32:11 字数 1255 浏览 0 评论 0原文

我有一个歌曲数组列表,其中存储标题、艺术家和时间 我已经写好了所有的代码和一切。我只是想了解更多关于 Collection.sort 和 Collection.reverse 和 Collection.reverseOrder 的信息。我有一个包含所有歌曲和所有内容的文件。 我想按照时间降序对歌曲进行排序。

当我尝试这样做时,我要么收到错误,要么排序不正确。谁能建议我如何 不能使用Collection.sort并使用比较器

Comparator<Song> comparator = Collections.reverseOrder();
Collections.reverse(listOfSongs);

我的比较方法如下:

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; 
    }

I have an Arraylist of songs which store title, artist and time
I have all my code written up and everything. I just wanted to know more about Collection.sort and Collection.reverse and Collection.reverseOrder. I have a file that has all the songs and everything.
I want to sort the songs according to descending order according to time.

When I try this I either get an error or it doesnt sort properly. can anyone suggest how I
cant use Collection.sort and use the comparator

Comparator<Song> comparator = Collections.reverseOrder();
Collections.reverse(listOfSongs);

My compare method is as follows:

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; 
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城 2024-12-09 01:32:11

Collections.reverse 不排序。它只是颠倒列表元素的顺序。因此,如果列表包含 T, F, Z,它将包含 Z, F, T

您的代码片段初始化了一个比较器,但不对其执行任何操作。这个比较器只能工作,如 javadoc 说,如果集合的元素实现了 Comparable 接口。

您应该使 Song 类实现 Comparable 接口(查看其 javadoc 以了解该接口必须执行的操作),或者必须使用特定的 Comparator< /代码>实施。无论选择哪种解决方案,您都必须实现一些代码来告诉 Collections.sort 歌曲如何相互比较。当一首歌的标题出现在另一首歌的标题之前时,这首歌是否在另一首歌之前?或者是当它的持续时间比另一个短时?

请参阅根据姓名对联系人 ArrayList 进行排序?

Collections.reverse doesn't sort. It just reverse the order of the elements of the list. So if the list contains T, F, Z, it will contain Z, F, T.

Your code snipper initialize a comparator, but doesn't do anything with it. And this comparator only works, as the javadoc says, if the elements of the collection implement the Comparable interface.

You should either make the Song class implement the Comparable interface (look at its javadoc to know what this interface must do), or you must use a specific Comparator implementation. Whatever the chosen solution is, you'll have to implement some code to tell Collections.sort how the songs compare with each other. Is a song before another one when its title comes before the other song's title? Or is it when its duration is shorter than the other one?

See Sorting an ArrayList of Contacts based on name?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文