Write a program to sort object using Comparator and Comparable

We will sort object of Student class. We will sort students by marks in ascending order.

Comparable:


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Student implements Comparable{
 private Integer id;
 private String name;
 private int marks;
 
 public Student() {
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public Student(Integer id, String name, int marks) {
  this.id = id;
  this.name = name;
  this.marks = marks;
 }


 @Override
 public int compareTo(Student stu) {
  if(this.marks < stu.marks){
   return -1;
  }else if(this.marks > stu.marks){
   return 1;
  }else{
   return 0;
  }
 }
 
 public static void main(String[] args) {
  List students = new ArrayList<>();
  students.add(new Student(1, "A", 67));
  students.add(new Student(2, "B", 35));
  students.add(new Student(3, "C", 80));
  students.add(new Student(4, "D", 17));
  System.out.println("Elements before sorting");
  for (Student student : students) {
   System.out.println("id = "+ student.id + 
     " name = "+ student.name + 
     " marks = "+ student.marks);
  }
  Collections.sort(students);
  System.out.println("Elements after sorting");
  for (Student student : students) {
   System.out.println("id = "+ student.id +
     " name = "+ student.name +
     " marks = "+ student.marks);
  }
 }
 
  
}

OUTPUT :
Elements before sorting
id = 1 name = A marks = 67
id = 2 name = B marks = 35
id = 3 name = C marks = 80
id = 4 name = D marks = 17
Elements after sorting
id = 4 name = D marks = 17
id = 2 name = B marks = 35
id = 1 name = A marks = 67
id = 3 name = C marks = 80

Comparator


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Student{
 private Integer id;
 private String name;
 private int marks;
 
 public Student() {
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public Student(Integer id, String name, int marks) {
  this.id = id;
  this.name = name;
  this.marks = marks;
 }


 public int getMarks() {
  return marks;
 }
 
 public static void main(String[] args) {
  List students = new ArrayList<>();
  students.add(new Student(1, "A", 67));
  students.add(new Student(2, "B", 35));
  students.add(new Student(3, "C", 80));
  students.add(new Student(4, "D", 17));
  System.out.println("Elements before sorting");
  for (Student student : students) {
   System.out.println("id = "+ student.id + 
     " name = "+ student.name + 
     " marks = "+ student.marks);
  }
  Collections.sort(students, new StudentComparator());
  System.out.println("Elements after sorting");
  for (Student student : students) {
   System.out.println("id = "+ student.id +
     " name = "+ student.name +
     " marks = "+ student.marks);
  }
 }
}

class StudentComparator implements Comparator{

 @Override
 public int compare(Student stu1, Student stu2) {
  if(stu1.getMarks() < stu2.getMarks()){
   return -1;
  }else if(stu1.getMarks() > stu2.getMarks()){
   return 1;
  }else{
   return 0;
  }
 }
 
}
OUTPUT:
Elements before sorting
id = 1 name = A marks = 67
id = 2 name = B marks = 35
id = 3 name = C marks = 80
id = 4 name = D marks = 17
Elements after sorting
id = 4 name = D marks = 17
id = 2 name = B marks = 35
id = 1 name = A marks = 67
id = 3 name = C marks = 80

Comments

Popular posts from this blog

Data types and Literals in Java

How to define Auxiliary Constructor in case class in Scala

Dependency Injection in Spring framework