0

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

2
  • 4
    So, where is the question and/or your problem to do so? Commented Aug 25, 2015 at 7:43
  • Use multiple lists or a SortedMap<ID, List<Code>> along with Collections.sort() and some Comparators. Commented Aug 25, 2015 at 7:46

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

This will group ClassVO object, but sorting will not be assured to be in specific order (A1,B1,C1,D1). Order may differ according to the values store in ClassVO (For example, if ClassVO having data in B1,D1,C1,A1 order it will correctly group the code but, sorting order will be like B1,D1,C1,A1 but it is expected to be in A1,B1,C1,D1)
Both your original question and your reply are quite confusing. Can you please provide more detail with your ClassVO structure and what you would actually like to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.