I'm starting with Java, and my problem is that I have an Arraylist of objects("Articulos", means Articles), and this object has properties (like name, price, etc...). I want to sort the Arraylist by the price of each object. Here's what I've tried.
First, I fill the Arraylist manually:
public static void Introducir_Articulo(){
Articulo a=new Articulo();
System.out.println("Codigo del articulo: "+(art.size()+1));
if(a.codArt==0){
a.codArt++;
} else {
a.codArt+=art.size();
}
System.out.println("Nombre del articulo: ");
a.NombreArt=sc.next();
System.out.println("Precio del articulo: ");
a.precio=sc.nextInt();
System.out.println("IVA del articulo: ");
a.iva=sc.nextInt();
art.add(a);
}
Later, I tried this
//copying the arraylist, so I don't have to change the original
artOrdenado=new ArrayList<Articulo>(art);
System.out.println(artOrdenado);
Collections.sort(artOrdenado, new Comparator<Articulo>(){
public int compare(Articulo uno, Articulo otro){
return uno.getPrecio().compareTo(otro.getPrecio());
}
});
but it throws an exception which says "int can not be deferenced".