user2000189
user2000189

Reputation: 519

Java Class Object Sorting and Group

I want to sort list of ClassVO object as follows,

ClassVO parameters -> id, code

Sample data:

ID  Code
A1  A1,AA1
B1  B1,BB1
C1  C1,CC1
D1  D1,DD1
B1  B2,BB2
A1  A2,AA2
C1  C2,CC2
D1  D2,DD2

In my ClassVO list objects the above sample data are added in unsorted way. Forexample, list-1 contains B1, list-1 contains D1, list-1 contains A1, list-1 contains C1. Order will vary each time.

Now i want to sort my ClassVO list objects as above. ie, before i am sorting my records, i want to group in such a order (First i want to group A1's code, then B1's code, C1's code and D1's code - While grouping the code it should be in ascending order). After grouping this i want my classVO list object like A1,B1,C1,D1.

Edit:

I am pasting my code here

public static void main(String[] args) {
    List<TestVo> vos = new ArrayList<TestVo>();
    TestVo testVo1 = new TestVo();
    testVo1.setCode("b");
    testVo1.setId("B_NIT_DEPT");
    vos.add(testVo1);
    TestVo testVo2 = new TestVo();
    testVo2.setId("C_NIT_FUNC");
    testVo2.setCode("NIT3");
    vos.add(testVo2);
    TestVo testVo4 = new TestVo();
    testVo4.setCode("NIT4d");
    testVo4.setId("D_NIT_DEPT");
    vos.add(testVo4);
    TestVo testVo = new TestVo();
    testVo.setCode("NIT1");
    testVo.setId("A_NIT");
    vos.add(testVo);
    TestVo testVo3 = new TestVo();
    testVo3.setCode("a");
    testVo3.setId("B_NIT_DEPT");
    vos.add(testVo3);
    Collections.sort(vos, new MyComparable());
    for (TestVo obj: vos) {
      System.out.println(obj.getId());
      System.out.println(obj.getCode());
    }
  }


public class MyComparable implements Comparator<TestVo>{
    @Override
    public int compare(TestVo vo1, TestVo vo2) {
        int ret =  0;
        if(vo1.getId().compareTo(vo2.getId()) != 0){
             ret =  vo1.getId().compareTo(vo2.getId());
        }
        return ret;
    }   
} 

My output is:

identifier=A_NIT
Code=NIT1
identifier=B_NIT_DEPT
Code=b
identifier=B_NIT_DEPT
Code=a
identifier=C_NIT_FUNC
Code=NIT3
identifier=D_NIT_DEPT
Code=NIT4d

But My expected output is :

identifier=A_NIT
Code=NIT1
identifier=B_NIT_DEPT
Code=a
identifier=B_NIT_DEPT
Code=b
identifier=C_NIT_FUNC
Code=NIT3
identifier=D_NIT_DEPT
Code=NIT4d

I want to make my output as given in expected output section

Upvotes: 0

Views: 99

Answers (2)

isurujay
isurujay

Reputation: 1446

You can do this either using java Comparable or Comparator. If you are using Java 8 then you don't need to implement compare or compareTo methods in a separate class. You can easily use lambda expressions to do this.

Collections.sort(listOfClassVO , (ClassVO vo1, ClassVO vo2) -> vo1.getID().compareTo(vo2.getID()));

Thanks

Upvotes: 1

McNultyyy
McNultyyy

Reputation: 1032

Make your ClassVO class implement the Comparable interface.

public class ClassVO implements Comparable {
....

    public int compareTo(ClassVO other) {
        return (this.getID().compareTo(other.getID()));
    }
}

And then you can just call Collections.sort on your list.

Upvotes: 0

Related Questions