2014-10-16 113 views
0

Im遵循MKyong sort examplesSorting a Array of Objects throws long [] error

我有類覆蓋compareTo()方法排序我的類riesgoTotal雙。 Sort array of object by particular member inside the object/class

public class AGRinformesActivos extends EntidadBase implements Comparable<AGRinformesActivos>{ 

private static final long serialVersionUID = -4341418821044868087L; 

private String     tipoActivo   = ""; 
private String     codigo    = ""; 
private String     denominacion  = ""; 
private int      idAmenaza   = 0; 
private double     impactoTotal  = 0; 
private double     riesgoTotal   = 0; 
private int      valorPendiente  = 0; 
ArrayList<Double>    impactoCuali  = new ArrayList<Double>(); 
ArrayList<String>    impactoCuanti  = new ArrayList<String>(); 
ArrayList<Double>    riesgoCuali   = new ArrayList<Double>(); 
ArrayList<String>    riesgoCuanti  = new ArrayList<String>(); 

要使用此compareTo()版本遞增進出口秩序。

public int compareTo(AGRinformesActivos o) { 
    return new Double(this.riesgoTotal).compareTo(new Double(o.riesgoTotal)); 
} 

這是我的代碼:

ArrayList <AGRinformesActivos> datosTotal = new ArrayList<AGRinformesActivos>(); 
    List listadoActTotal = manager.dameListadoActivos(Integer.parseInt(info.getIdtipoActivo().trim()), info.getCodigo().trim()); 
for(Object o : listadoActTotal) { 
       fila = (List) o;      
       String codigoActivo = fila.get(0).toString();    
       // If exists we obtain, if not exists we create. 
       if (activosDimension.containsKey(codigoActivo)){ 
        // If exists, obtain from activosDimension 
        informesActivo = activosDimension.get(codigoActivo); 
       }else{ 
        informesActivo = new AGRinformesActivos(); 
        activosDimension.put(codigoActivo, informesActivo); 
        // Al objeto le añado tipoActivo, idActivo y Denominación 
        String tipoActivo = manager.dameNombreTipoActivo(info.getIdtipoActivo()); 
        informesActivo.setTipoActivo(tipoActivo); 
        informesActivo.setCodigo(codigoActivo); 
        informesActivo.setDenominacion(fila.get(1).toString()); 
        } 
        // Add impactoTotal and riesgoTotal 
        informesActivo.setImpactoTotal(Double.parseDouble(fila.get(2).toString())); 
        informesActivo.setRiesgoTotal(Double.parseDouble(fila.get(3).toString())); 
       } 

       informesActivo.getImpactoCuali().add(Double.parseDouble(fila.get(5).toString())); 
       informesActivo.getRiesgoCuali().add(Double.parseDouble(fila.get(6).toString())); 

       String gradoImp = dameRangoEconGrado(Double.parseDouble(fila.get(5).toString())); 
       String gradoRiesg = dameRangoEconGrado(Double.parseDouble(fila.get(6).toString()));    
       if(gradoImp.equals("")){ 
        gradoImp = dameRangoEconGrado(1); 
       } 
       informesActivo.getImpactoCuanti().add(gradoImp); 
       if(gradoRiesg.equals("")){ 
        gradoRiesg = dameRangoEconGrado(1); 
       } 
       informesActivo.getRiesgoCuanti().add(gradoRiesg); 

       if (!datosTotaltemp.contains(informesActivo)){ 
        datosTotaltemp.add(informesActivo); 
        datosTotal.add(informesActivo); 
       } 
      } 
Arrays.sort(datosTotal); 

... 

當代碼達到Arrays.sort(datosTotal);引發錯誤:

The method sort(long[]) in the type Arrays is not applicable for the arguments (ArrayList<AGRinformesActivos>) 
] con causa raíz 
java.lang.Error: Unresolved compilation problem: 
    The method sort(long[]) in the type Arrays is not applicable for the arguments (ArrayList<AGRinformesActivos>) 

    at com.dominion.procop.agr.struts.actions.AGRInformes.buscarActivosEmpty(AGRInformes.java:270) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 

我到底做錯了什麼? AGRinformesActivos有一個很長的靜態。這是問題嗎?爲什麼?在我的compareTo即時通訊使用riesgoTotal的雙倍。

預先感謝您

+1

您正在對「集合」進行排序,而不是數組。嘗試'Collections.sort()'而不是'Arrays.sort()'。 – Keppil 2014-10-16 11:22:33

+0

^^並提供'比較器''''Collections.sort(datosTotal,new Comparator ...'; – 2014-10-16 11:23:40

+1

「類型Arrays中的方法sort(long [])不適用於參數(ArrayList )「!您可以使用'ArrayList'調用sort方法,但期望使用'long []'。並且...不要運行帶有編譯錯誤的代碼 – Seelenvirtuose 2014-10-16 11:24:10

回答

2

您正在對一個Collection進行排序,而不是一個數組。嘗試Collections.sort()而不是Arrays.sort()

+0

謝謝,我沒有看到錯誤。 – 2014-10-16 11:45:18

2

你的datosTotal引用是一個集合,而不是一個數組。所以你不應該使用Arrays來排序它,而是Collections.sort

出於效率的緣故,我也建議你使用

public int compareTo(AGRinformesActivos o) { 
    return Double.compare(this.riesgoTotal,o.riesgoTotal); 
} 

而不是爲每個比較兩個新的雙重目標。