import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeSet;
public class SortMethods {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet al_st = new TreeSet(new StudentHeight());
// ArrayList al_st = new ArrayList();
Student s1 = new Student(1,"Jitesh", 12,5.9f);
al_st.add(s1);
Student s2 = new Student(2,"Amit", 11,5.7f);
al_st.add(s2);
Student s3 = new Student(3,"Rohan", 10,5.8f);
al_st.add(s3);
for(Student s4: al_st)
{
System.out.println(s4.roll_no+","+s4.name+","+s4.height);
}
}
}
class Student implements Comparable{
int roll_no;
String name ;
int std ;
float height ;
Student(int r1, String n1, int s1, float h1){
this.roll_no = r1 ;
this.name = n1 ;
this.std = s1 ;
this.height = h1 ;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
Student sx = (Student) o ;
return this.name.compareTo(sx.name);
// return (s1.roll_no - this.roll_no);
}
}
class StudentHeight implements Comparator{
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
if ((o1.height-o2.height)>0){
return 1 ;
}
else if ((o1.height-o2.height)<0){
return -1 ;
}
else {
return 0 ;
}
// return (int) (o1.height-o2.height);
}
}
|