2011-12-15 22 views
1

我有權命令的集合,我用一個比較比較未發現

import java.util.*; 

public class OpereComparatorAuthor implements Comparator<Opera>{ 

    public int compare(Opera left,Opera right){  
     return left.getArtist().compareTo(right.getArtist()); 
    } 
} 

,但是當我從另一個類調用它:

Collections.sort(ordbyauthor,OpereComparatorAuthor); 

我收到此錯誤:

cannot find symbol 
symbol : variable OpereComparatorAuthor 
location: class Museo 
    Collections.sort(ordbyauthor,OpereComparatorAuthor); 

爲什麼?

+0

您在評論中提及Salla。 Opera和Salla有什麼關係?此外,發佈更多代碼圍繞如何調用排序,包括創建對象後我收到錯誤的列表 – Bohemian 2011-12-15 20:38:07

回答

3

你要通過比較的對象:

Collections.sort(ordbyauthor, new OpereComparatorAuthor()); 

更新:

這僅僅是一個建議。對於這種情況,不要定義班級,而應該使用匿名班級(我認爲這是一個很好的選擇)。

例如

//I am using Sala here instead of Opera as per your comment 
Collections.sort(ordbyauthor, new Comparator<Sala>(){ 
    @Override 
    public int compare(Sala left, Sala right) { 
     //do your comparision here according to your requirement 
     //then return the result 
    } 
}); 
+0

的定義:無法找到符號 符號:方法排序(java.util.LinkedList ,OpereComparatorAuthor) location :class java.util.Collections \t \t Collections.sort(ordbyauthor,new OpereComparatorAuthor()); – Mazzy 2011-12-15 20:03:03