1. Arrays.sort(Array_of_your_obj);
2. Using Comparable Interface
3. Using Comparator Interface
Arrays.soft(Array_of_your_obj)
Usually we do sorting of the array of string using this arrays.sort. You can not use any custome object like student, account( you will get classcastException).
Example is given below to use Arrays.Sort()
String names[] = {"USA","Binod","Satyam","Java","Dollar","Japan"};
Arrays.sort(names);
for(String str:names){
System.out.println("NAME :: "+str);
}
Output would be:
NAME :: Binod
NAME :: Dollar
NAME :: Japan
NAME :: Java
NAME :: Satyam
NAME :: USA
Using Comparable Interface
1. Student.java
import java.util.Comparator;
public class Student implements Comparable{
int roll;
String name;
int age;
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
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;
}
public Student(int roll, String name, int age) {
this.roll = roll;
this.name = name;
this.age = age;
}
public int compareTo(Object o) {
return ((Student)o).getRoll() - this.roll;
// For Decening Order
return this.roll - ((Student)o).getRoll();
//return this.age - ((Student)o).getAge();
}
}
2. Client.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Client {
public static void main(String[] args) {
Client c = new Client();
List students = c.getStudent();
// If Student implemnets comparable then work otherwise give class cast exception
Collections.sort(students);
c.print(students);
}
public List getStudent(){
List result = new ArrayList();
result.add(new Student(110,"Binod",25));
result.add(new Student(102,"Pramod",23));
result.add(new Student(130,"Ambani",12));
result.add(new Student(140,"Mittal",6));
result.add(new Student(125,"Binod",20));
return result;
}
public void print(List
for(Student s: list){
System.out.println(s.getRoll()+" "+s.getName()+" "+s.getAge());
}
}
}
OUTPUT:
102 Pramod 23
110 Binod 25
125 Binod 20
130 Ambani 12
140 Mittal 6
Using Comparator Interface
1. Student.java
public class Student {
int roll;
String name;
int age;
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
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;
}
public Student(int roll, String name, int age) {
this.roll = roll;
this.name = name;
this.age = age;
}
}
3. StudentSortByName.java
import java.util.Comparator;
public class StudentSortByName implements Comparator
public int compare(Student o1, Student o2) {
/*int name = o1.getName().compareTo(o2.getName());
if(name!=0)return name;
else return (o1.getAge() - o2.getAge());*/
return o1.getName().compareTo(o2.getName());
}
}
3. Client.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Client {
public static void main(String[] args) {
Client c = new Client();
List students = c.getStudent();
Collections.sort(students,new StudentSortByName());
c.print(students);
}
public List getStudent(){
List result = new ArrayList();
result.add(new Student(110,"Binod",25));
result.add(new Student(102,"Pramod",23));
result.add(new Student(130,"Ambani",12));
result.add(new Student(140,"Mittal",6));
result.add(new Student(125,"Binod",20));
return result;
}
public void print(List
for(Student s: list){
System.out.println(s.getRoll()+" "+s.getName()+" "+s.getAge());
}
}
}
OUTPUT :
130 Ambani 12
110 Binod 25
125 Binod 20
140 Mittal 6
102 Pramod 23
comparable v comparator
A Comparable interfaced class must contain a method called compareTo to compare two objects (one being the object on which it is called and the other being passed as a paramater) which returns an integer, negative for a <> b and 0 otherwise. It must also contain an equals method, returning a boolean, on the same parameters.
A Comparator is a class in its own right, which implements the Comparator interface; that means it must contain a method called compare (two objects as parameters) which returns a negative, zero or positive integer depensing on whether the first object is less than, equal to, or greater than the second.
Comparable interface shows that the class knows how to compare itself against another class.Comparator interface allows a single class know how to compare two classes which (typically) are not the same type as the comparator.
If you do not have access to change existing file then you could not use comparable. Then you will have only option for comparator interface.