Collection.sort/reverse/reverOrder
我有一个歌曲数组列表,其中存储标题、艺术家和时间 我已经写好了所有的代码和一切。我只是想了解更多关于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 containsT, F, Z
, it will containZ, 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 theComparable
interface (look at its javadoc to know what this interface must do), or you must use a specificComparator
implementation. Whatever the chosen solution is, you'll have to implement some code to tellCollections.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?