i have to sort candiadates on the basics of both name and age .here the data
Arun 20 Bucky 22 Arun 25
and the output has to be
Arun 25 Arun 20 Bucky 22
here the coding
import java.util.Comparator;
public class Student implements Comparable<Student> {
int id;
String name;
int age;
public int compareTo(Student s1) {
return this.age-s1.age;
}
//Constructor
public Student(String name,int id,int age) {
// TODO Auto-generated constructor stub
this.name="";
this.id=0;
this.age=0;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Here the main class
import java.util.ArrayList;
import java.util.Collections;
public class StudentList {
public static void main(String args[]){
ArrayList<Student> list=new ArrayList<Student>();
Student s1= new Student(null, 0, 0);
s1.setName("Andy");
s1.setAge(25);
s1.setId(1);
Student s2=new Student(null, 0, 0);
s2.setName("Brad");
s2.setAge(22);
s2.setId(2);
Student s3=new Student(null, 0, 0);
s3.setName("Andy");
s3.setAge(30);
s3.setId(3);
list.add(s1);
list.add(s2);
list.add(s3);
Collections.sort(list);
for(Student a:list){
System.out.println(a.getName()+""+a.getAge());
}
}
}