How To Sort Array Of Objects Using Comparator Interface In java
We all know that we can easily sort an Array using sort() method of utility class java.util.Arrays.But if we want to sort an Array of objects based on the instance variable of the object like id, name or some other instance variable then we ca use Comparator interface.For using Comparator we will use a overridden method of sort() from java.util.Arrays class.
sort(array,comparator)
This method takes two arguments one ,the array we want to sort and the other one is the instance of Comparator.
Comparator interface has a method compare() which compares two objects and returns an integer value based on the following table-
int compare(object1,object2)
We will use this logic for writing our code for Comparator instance.
Now we are ready to write the code, so here are the classes we are going to write-
Class 1-Student.java
This comparator instance implements java.util.Comparator interface and is used to sort the student objects based on the id.
This is another comparator which is used to sort the objects on the basis of name which is a string.For comparing String we can use compareTo() method of Comparable interface,which does the same thing as compare() in Comparator.
In this class we have created a Array of student objects and put some values using constructor.First we have printed the objects without sorting and then we will print the objects after sorting based on id and name respectively.
id is 1 name is Alan age is 21
id is 22 name is Bob age is 21
id is 14 name is Leo age is 22
id is 4 name is Merry age is 18
id is 56 name is Sam age is 31
id is 33 name is Zues age is 41
id is 44 name is Rock age is 11
id is 2 name is John age is 27
id is 1 name is Alan age is 21
id is 2 name is John age is 27
id is 4 name is Merry age is 18
id is 14 name is Leo age is 22
id is 22 name is Bob age is 21
id is 33 name is Zues age is 41
id is 44 name is Rock age is 11
id is 56 name is Sam age is 31
id is 1 name is Alan age is 21
id is 22 name is Bob age is 21
id is 2 name is John age is 27
id is 14 name is Leo age is 22
id is 4 name is Merry age is 18
id is 44 name is Rock age is 11
id is 56 name is Sam age is 31
id is 33 name is Zues age is 41
So this was the simple program showing how to sort an Array of objects using Comparator interface.We can also use comparable interface for this kind of sorting but there are some limitations in that, which you will learn in our next article.
sort(array,comparator)
This method takes two arguments one ,the array we want to sort and the other one is the instance of Comparator.
Comparator interface has a method compare() which compares two objects and returns an integer value based on the following table-
int compare(object1,object2)
if object1<object2 | negative value |
if object1>object2 | positive value |
if object1=object2 | zero(0) |
Now we are ready to write the code, so here are the classes we are going to write-
- First create a class which objects we need to put in the Array for sorting
- Comparator instances used for sorting based on different instace variables of the Class objects we created..
- A test class where lies the main method to start the execution.
Class 1-Student.java
package in.blogger.tutorialsinn; public class Student { private int id; private String name; private int age; //constructor public Student(int id, int age, String name) { super(); this.id = id; this.name = name; this.age = age; } //overriding toString() method for printing the object values @Override public String toString() { return ("id is " + id + " name is " + name + " age is " + age); } //setters and geters 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; } }Class-2 StudentIdComparator.java
This comparator instance implements java.util.Comparator interface and is used to sort the student objects based on the id.
package in.blogger.tutorialsinn; import java.util.Comparator; import in.blogger.tutorialsinn.Student; public class StudentIdComparator implements Comparator { @Override public int compare(Object o1, Object o2) { Student s1 = (Student) o1; Student s2 = (Student) o2; if(s1.getId()>s2.getId()){ return 1; } else if(s1.getId()<s2.getId()){ return -1; } else return 0; } }Class 3 StudentNameComparator.java
This is another comparator which is used to sort the objects on the basis of name which is a string.For comparing String we can use compareTo() method of Comparable interface,which does the same thing as compare() in Comparator.
package in.blogger.tutorialsinn; import java.util.Comparator; import in.blogger.tutorialsinn.Student; public class StudentNameComparator implements Comparator { @Override public int compare(Object o1, Object o2) { Student s1 = (Student) o1; Student s2 = (Student) o2; return s1.getName().compareTo(s2.getName()); } }Class-4 Testing.java
In this class we have created a Array of student objects and put some values using constructor.First we have printed the objects without sorting and then we will print the objects after sorting based on id and name respectively.
package in.blogger.tutorialsinn; import java.lang.reflect.Array; import java.util.*; import in.blogger.tutorialsinn.StudentIdComparator; import in.blogger.tutorialsinn.StudentNameComparator; public class Testing { public static void main(String[] args) { Student s1=new Student(1,21,"Alan"); Student s2=new Student(22,21,"Bob"); Student s3=new Student(14,22,"Leo"); Student s4=new Student(4,18,"Merry"); Student s5=new Student(56,31,"Sam"); Student s6=new Student(33,41,"Zues"); Student s7=new Student(44,11,"Rock"); Student s8=new Student(2,27,"John"); Student students[]={s1,s2,s3,s4,s5,s6,s7,s8}; for(int i=0;i<students.length;i++){ System.out.println(students[i]); } //sorting based on id Arrays.sort(students,new StudentIdComparator()); for(int i=0;i<students.length;i++){ System.out.println(students[i]); } //sorting based on name Arrays.sort(students,new StudentNameComparator()); for(int i=0;i<students.length;i++){ System.out.println(students[i]); } } }Output-
id is 1 name is Alan age is 21
id is 22 name is Bob age is 21
id is 14 name is Leo age is 22
id is 4 name is Merry age is 18
id is 56 name is Sam age is 31
id is 33 name is Zues age is 41
id is 44 name is Rock age is 11
id is 2 name is John age is 27
id is 1 name is Alan age is 21
id is 2 name is John age is 27
id is 4 name is Merry age is 18
id is 14 name is Leo age is 22
id is 22 name is Bob age is 21
id is 33 name is Zues age is 41
id is 44 name is Rock age is 11
id is 56 name is Sam age is 31
id is 1 name is Alan age is 21
id is 22 name is Bob age is 21
id is 2 name is John age is 27
id is 14 name is Leo age is 22
id is 4 name is Merry age is 18
id is 44 name is Rock age is 11
id is 56 name is Sam age is 31
id is 33 name is Zues age is 41
So this was the simple program showing how to sort an Array of objects using Comparator interface.We can also use comparable interface for this kind of sorting but there are some limitations in that, which you will learn in our next article.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment