简体   繁体   中英

Sorting an Array of Objects throws long[] error

Im following the MKyong sort examples .

I have the class AGRinformesActivos overriding compareTo() method to sort my class by riesgoTotal double.

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>();

To order in asc Im using this compareTo() version: Sort array of object by particular member inside the object/class .

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

And this is my code:

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

...

When the code reach to Arrays.sort(datosTotal); throws the error:

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)

What am i doing wrong? AGRinformesActivos has a static long. is this the problem? why? in my compareTo im using the double of riesgoTotal .

Thank you in advance

You are sorting a Collection, not an array. Try Collections.sort() instead of Arrays.sort() .

your datosTotal reference is a collection, not an array. So you shouldn't use Arrays to sort it, but rather Collections.sort .

For efficiency's sake, I'd also recommend that you use

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

Rather than creating two new Double objects for every comparison.

Arrays.sort can not sort an ArrayList. Try to use Collections.sort

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM