Open nehatomer37 opened 2 years ago
import java.util.ArrayList; import java.util.Collections;
class Vehicle implements Comparable { private String vname; private String brand; private long price;
public Vehicle(String vname, String brand, long price) { this.vname = vname; this.brand = brand; this.price = price; } public String toString() {
return vname + "" +brand+ ""+ price;
} @Override public int compareTo(Vehicle o) { // TODO Auto-generated method stub return this.vname.compareTo(o.vname);
}
} class SortArraylist{ public static void main(String args[]){ Vehicle v1=new Vehicle("Tata Harrier","Tata",2000000); Vehicle v2=new Vehicle("Audi Q5","Audi",6000000); Vehicle v3=new Vehicle("Baleno","Maruti Suzuki",1000000);
ArrayList v =new ArrayList(); v.add(v1); v.add(v2); v.add(v3); System.out.println("Elements in the List" + v); Collections.sort(v);
System.out.println("Sorted List by name" + v);
} }
import java.util.ArrayList; import java.util.Collections;
class Vehicle implements Comparable
{
private String vname;
private String brand;
private long price;
public Vehicle(String vname, String brand, long price) { this.vname = vname; this.brand = brand; this.price = price; } public String toString() {
} @Override public int compareTo(Vehicle o) { // TODO Auto-generated method stub return this.vname.compareTo(o.vname);
}
} class SortArraylist{
public static void main(String args[]){
Vehicle v1=new Vehicle("Tata Harrier","Tata",2000000);
Vehicle v2=new Vehicle("Audi Q5","Audi",6000000);
Vehicle v3=new Vehicle("Baleno","Maruti Suzuki",1000000);
ArrayList v =new ArrayList();
v.add(v1);
v.add(v2);
v.add(v3);
System.out.println("Elements in the List" + v); Collections.sort(v);
} }