Sort objects in a collection, sorting ascending or descending order according to the size of an indicator of the object. The code is as follows:
Order descending order
Conduct descending order sorting Collections.sort(list, new Comparator<ResultTypeDesc>() { public int compare(ResultTypeDesc o1, ResultTypeDesc o2) { return o2.getRatio().compareTo(o1.getRatio()); } });Arrange ascending order
Collections.sort(list, new Comparator<ResultTypeDesc>() {public int compare(ResultTypeDesc o1, ResultTypeDesc o2) {return o1.getRatio().compareTo(o2.getRatio()); }});After testing, it was found that you only need to change the positions of the two objects to ascending or descending order.
If the metrics are the same, sort them according to multiple metrics and create a comparator:
import java.util.*;public class ComparatorResultType implements Comparator{ public int compare(Object arg0, Object arg1) { ResultTypeDesc desc0=(ResultTypeDesc)arg0; ResultTypeDesc desc1=(ResultTypeDesc)arg1; //First compare the main indicators. If the main indicators are the same, then compare the secondary indicators int flag=desc0.getXXX().compareTo(desc1.getXXX()); if(flag==0){ return desc0.getXXX2().compareTo(desc1.getXXX2()); }else{ return flag; } }}//Code in the test class: ComparatorResultType comparator=new ComparatorResultType();Collections.sort(list, comparator);Inverse output of list set:
Collections.reverse(list);
ResultTypeDesc is the required entity class object, and you can use it in combination with your own code.
This method may report a null pointer, and solve it yourself by combining the situation, and determine whether it is NULL.