Saturday, May 9, 2015

How to sort the object based on Id and Name in Java

If we have the List, Map or any other collection object we sort by using calling Collections.sort() method directly. But if we have our custom object we have to do some extra to sort our custom object. Assume that we have one Person Object like below

class Person{
    int id;
    String name;

    Setter…
    Getter…
}




We have two way to sort the object-

1-    We can sort our object by using Comparable interface but this having limitation to sort based on only one member either id or name (In our case).
2-    If we choose Comparator interface we can sort the object in all the way (id and name both).
So here I’m using 2 way to sort our Person class object.

Step 1- Create Person class.

Step 2- Create two class SortById and SortByName in client program (MyCompartor.java).

Step 3- Create some instance of Person object with value.

Step 4- Call the Collections.sort(personList, new SortById()); or Collections.sort(personList, new SortByName()); to sort the object base on id and name respectively.

Example:

Person.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package com.atozjavatutorials;

public class Person {
    private int id;
    private String name;
    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;
    }
}

MyCompartor.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.atozjavatutorials;

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

public class MyCompartor  {
    public static void main(String args[]) {
        Person person1 = new Person();
        person1.setId(101);
        person1.setName("Ram");
        Person person2 = new Person();
        person2.setId(100);
        person2.setName("Shyam");
        Person person3 = new Person();
        person3.setId(103);
        person3.setName("Ashish");
        List<Person> personList = new ArrayList<Person>();
        personList.add(person1);
        personList.add(person2);
        personList.add(person3);     
        System.out.println("\nOriginol Object");
        System.out.println("===============");
        for (Person person : personList) {
           System.out.println(person.getId()+":"+person.getName());
        }
        System.out.println("\nSortById");
        System.out.println("===============");
        Collections.sort(personList, new SortById());
        for (Person person : personList) {
            System.out.println(person.getId()+":"+person.getName());
        }
        System.out.println("\nSortByName");
        System.out.println("===============");
        Collections.sort(personList, new SortByName());
        for (Person person : personList) {
            System.out.println(person.getId()+":"+person.getName());
        }
    }
}
class SortById implements Comparator<Person>{
    @Override
    public int compare(Person o1, Person o2) {
        int person_id1=o1.getId();
        int person_id2=o2.getId();
        if(person_id1 > person_id2){
            return 1;
        }
        else if(person_id1 < person_id2){
            return -1;
        }
        else return 0;
    }
}
class SortByName implements Comparator<Person>{
    @Override
    public int compare(Person o1, Person o2) {
        return o1.getName().compareTo(o2.getName());
    }
}

Output:

Originol Object
===============
101:Ram
100:Shyam
103:Ashish

SortById
===============
100:Shyam
101:Ram
103:Ashish

SortByName
===============
103:Ashish
101:Ram
100:Shyam

No comments :

Post a Comment