How to sort list based on its element value?
In my unit test class, I am receiving list which is in sorted form based on one of its element’s value {sortOrder and it has value 1, 2 and 3} now I am comparing returnedList with expectedList to make sure that both list are same and have same elements in same order.
Example of my return and expected list:
expected List = [Code: ANALYST, SortOrder: 2, Code: STREET, SortOrder: 1]
and returned List = [Code: STREET, SortOrder: 1, Code: ANALYST, SortOrder: 2]
So how can i sort the list on SortOrder so that my expected list becomes:
expected List = [Code: STREET, SortOrder: 1, Code: ANALYST, SortOrder: 2]
I cannot use any libraries and this question is related to Compare List Question, any suggestions?
use this:
So do something like this:
myComparator is an interface for which you only have to implement one method which is compare:
So you can do this with an anonymous inner class:
For information on how to implement the compare method take a look at the documentation here:
Comparator