package example;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo demo = new Demo();
List<Student> list = new ArrayList<Student>(); // your Car list
Student st = demo.new Student();
st.setName("sekhar");
st.setCity("bbsr");
st.setRoll(23);
list.add(st);
st = demo.new Student();
st.setName("himanshu");
st.setCity("agra");
st.setRoll(23);
list.add(st);
st = demo.new Student();
st.setName("nitin");
st.setCity("delhi");
st.setRoll(23);
list.add(st);
st = demo.new Student();
st.setName("nitin");
st.setCity("delhi");
st.setRoll(22);
list.add(st);
st = demo.new Student();
st.setName("nitin");
st.setCity("bbsr");
st.setRoll(23);
list.add(st);
st = demo.new Student();
st.setName("arun");
st.setCity("patna");
st.setRoll(23);
list.add(st);
st = demo.new Student();
st.setName("arun");
st.setCity("kendrapara");
st.setRoll(23);
list.add(st);
System.out.println("before sort");
System.out.println("Name:" + "-------------------" + "City...........roll");
for (Student st1 : list) {
System.out.println(st1.getName() + " --------------" + st1.getCity() + "------------" + st1.getRoll());
}
Collections.sort(list, demo.new CarHorsePowerComparator());
System.out.println("after sort");
System.out.println("Name:" + "-------------------" + "City...............roll");
for (Student st1 : list) {
System.out.println(st1.getName() + " --------------" + st1.getCity() + "------------" + st1.getRoll());
}
}
public class Student {
String name;
String city;
int roll;
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class CarHorsePowerComparator implements Comparator<Student> {
private final Collator collator = Collator.getInstance(); //Default local is taken.
public int compare(Student left, Student right) {
int result = collator.compare(left.getName(),right.getName());
if(result == 0) {
result = collator.compare(right.getCity(),left.getCity());
}
if(result == 0) {
result = Integer.compare(left.getRoll(), right.getRoll()); //JDK 7
//result = Double.compare(left.getRoll(), right.getRoll());
}
return result;
}
}
}
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo demo = new Demo();
List<Student> list = new ArrayList<Student>(); // your Car list
Student st = demo.new Student();
st.setName("sekhar");
st.setCity("bbsr");
st.setRoll(23);
list.add(st);
st = demo.new Student();
st.setName("himanshu");
st.setCity("agra");
st.setRoll(23);
list.add(st);
st = demo.new Student();
st.setName("nitin");
st.setCity("delhi");
st.setRoll(23);
list.add(st);
st = demo.new Student();
st.setName("nitin");
st.setCity("delhi");
st.setRoll(22);
list.add(st);
st = demo.new Student();
st.setName("nitin");
st.setCity("bbsr");
st.setRoll(23);
list.add(st);
st = demo.new Student();
st.setName("arun");
st.setCity("patna");
st.setRoll(23);
list.add(st);
st = demo.new Student();
st.setName("arun");
st.setCity("kendrapara");
st.setRoll(23);
list.add(st);
System.out.println("before sort");
System.out.println("Name:" + "-------------------" + "City...........roll");
for (Student st1 : list) {
System.out.println(st1.getName() + " --------------" + st1.getCity() + "------------" + st1.getRoll());
}
Collections.sort(list, demo.new CarHorsePowerComparator());
System.out.println("after sort");
System.out.println("Name:" + "-------------------" + "City...............roll");
for (Student st1 : list) {
System.out.println(st1.getName() + " --------------" + st1.getCity() + "------------" + st1.getRoll());
}
}
public class Student {
String name;
String city;
int roll;
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class CarHorsePowerComparator implements Comparator<Student> {
private final Collator collator = Collator.getInstance(); //Default local is taken.
public int compare(Student left, Student right) {
int result = collator.compare(left.getName(),right.getName());
if(result == 0) {
result = collator.compare(right.getCity(),left.getCity());
}
if(result == 0) {
result = Integer.compare(left.getRoll(), right.getRoll()); //JDK 7
//result = Double.compare(left.getRoll(), right.getRoll());
}
return result;
}
}
}
OutPut:
before sort
Name:-------------------City...........roll
sekhar --------------bbsr------------23
himanshu --------------agra------------23
nitin --------------delhi------------23
nitin --------------delhi------------22
nitin --------------bbsr------------23
arun --------------patna------------23
arun --------------kendrapara------------23
after sort
Name:-------------------City...............roll
arun --------------patna------------23
arun --------------kendrapara------------23
himanshu --------------agra------------23
nitin --------------delhi------------22
nitin --------------delhi------------23
nitin --------------bbsr------------23
sekhar --------------bbsr------------23
No comments:
Post a Comment