Comparator Interface Sorting a list by multiple values
Comparator Interface Sorting a list by multiple values. We have Student bean class. We want to sort the list Name, Age, RollNum.
package codding.time.comparator;
public class Student {
int stuid;
String stuName;
int age;
int rollNum;
Student(int stuid, String stuName, int age, int rollNum) {
this.stuid = stuid;
this.stuName = stuName;
this.age = age;
this.rollNum = rollNum;
}
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getRollNum() {
return rollNum;
}
public void setRollNum(int rollNum) {
this.rollNum = rollNum;
}
}
int stuid;
String stuName;
int age;
int rollNum;
Student(int stuid, String stuName, int age, int rollNum) {
this.stuid = stuid;
this.stuName = stuName;
this.age = age;
this.rollNum = rollNum;
}
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getRollNum() {
return rollNum;
}
public void setRollNum(int rollNum) {
this.rollNum = rollNum;
}
}
package codding.time.comparator;
import java.util.Comparator;
public class AgeSortComparator implements Comparator<Student> {
@Override
public int compare(Student obj1, Student obj2) {
int compareName = obj1.getStuName().compareTo(obj2.getStuName());
int compareAge = Integer.valueOf(obj1.getAge()).compareTo(Integer.valueOf(obj2.getAge()));
int compareRoll = Integer.valueOf(obj1.getRollNum()).compareTo(Integer.valueOf(obj2.getRollNum()));
if (compareName != 0) {
return compareName;
}
if (compareAge != 0) {
return compareAge;
}
return compareRoll;
}
}
public class AgeSortComparator implements Comparator<Student> {
@Override
public int compare(Student obj1, Student obj2) {
int compareName = obj1.getStuName().compareTo(obj2.getStuName());
int compareAge = Integer.valueOf(obj1.getAge()).compareTo(Integer.valueOf(obj2.getAge()));
int compareRoll = Integer.valueOf(obj1.getRollNum()).compareTo(Integer.valueOf(obj2.getRollNum()));
if (compareName != 0) {
return compareName;
}
if (compareAge != 0) {
return compareAge;
}
return compareRoll;
}
}
package codding.time.comparator;
import java.util.ArrayList;
import java.util.Collections;
public class StudentListSortByComparator {
public static void main(String args[]) {
ArrayList<Student> al = new ArrayList<Student>();
al.add(new Student(2, "A", 5, 30));
al.add(new Student(3, "A", 5, 20));
al.add(new Student(5, "B", 2, 10));
Collections.sort(al, new AgeSortComparator());
for (Student stu : al) {
System.out.println(stu.getStuid() + " " + stu.getStuName() + " " + stu.getAge() + " " + stu.getRollNum());
}
}
}
import java.util.Collections;
public class StudentListSortByComparator {
public static void main(String args[]) {
ArrayList<Student> al = new ArrayList<Student>();
al.add(new Student(2, "A", 5, 30));
al.add(new Student(3, "A", 5, 20));
al.add(new Student(5, "B", 2, 10));
Collections.sort(al, new AgeSortComparator());
for (Student stu : al) {
System.out.println(stu.getStuid() + " " + stu.getStuName() + " " + stu.getAge() + " " + stu.getRollNum());
}
}
}
output : -
3 A 5 20
2 A 5 30
5 B 2 10
post a comment