sorting - ArrayList sort issue -
i having issue code trying compare not working getting no suitable method found sort(int)
first class caru class contains method , second method in class
import java.io.*; public class caru extends car implements serializable { private string number; private int price; private string phone; public caru() { number = ""; price = 0; phone = ""; } public caru(string n1,int p,string pp,string ma,string mo,string br,int yo,string en) { super(ma,mo,br,yo,en); number = n1; price = p; phone = pp; } public void setnum(string n1) { number = n1; } public string getnum() { return number; } public void setprice(int p) { price = p; } public int getprice() { return price; } public void setphone(string pp) { phone = pp; } public string getphone() { return phone; } public string tostring() { return "\n car manufacturer: "+getmake()+ "\n phone: "+getphone()+ " price: €"+getprice()+ "\n car number: "+getnum()+ "\n car model: "+getmodel()+ "\n car type: "+gettype()+ "\n year of make: "+getyom()+ "\n car engine: "+geteng(); } }
public void lis() throws ioexception, classnotfoundexception{ file infile = new file("carus"); fileinputstream infilestream = new fileinputstream(infile); objectinputstream inobjectstream = new objectinputstream(infilestream); list carlist; carlist = new arraylist(); try { carlist = (list)inobjectstream.readobject(); } catch (exception z){ system.out.println("no cars available"); } finally{ inobjectstream.close(); (int i=0;i<carlist.size();i++) { caru temp = (caru)carlist.get(i); collections.sort(temp.getprice()); system.out.println((caru)carlist.get(i)); system.out.println("============================"); } } }
you need create custom comparator below.
package com.expertwebindia; public class pricecomparator extends comparator<caru> { public int compare(caru c1, caru c2) { return c1.getprice()-c2.getprice(); } }
pass in collections.sort() method below.
collections.sort(carlist,new pricecomparator());
Comments
Post a Comment