Java : How to sort Array List based on Custom Order

How to sort Array List using Custom Order

On many occasions we need to sort an array list or linked list based on custom logic. Suppose you have a Quiz application and you want to return questions based on a custom question id.

e.g Request : ids = [ 88, 21, 43, 15, 64, 35 ]

When you will fetch the Questions from Database you will be using a query something like below

select * from question where id in (?)

This will give you questions ordered by id by default. So to covert it in same order as requested order you can use

  1. // ids = [ 88, 21, 43, 15, 64, 35 ]  
  2. // questions = new ArrayList<Question>();   
  3. Collections.sort(questions, new Comparator<Question>() {  
  4.      @Override  
  5.      public int compare(QuestionDto q1, QuestionDto q2) {  
  6.          // compare based on the index of id in list **ids  
  7.          return ids.indexOf(q1.getId()) - ids.indexOf(q2.getId());  
  8.      }  
  9.  });  

The above code will give you questions in order of ids requested.




No comments :

Post a Comment