一个比较器,为了做排序对象所用,一个通用的例子
?
?
class="java" name="code">public interface Order { public int getOrderIndex() ; }
?
?
import java.util.Comparator; /** * 升序排序 * */ public class OrderAscStorComparator implements Comparator<Order> { @Override public int compare(Order o1, Order o2) { if (o1.getOrderIndex() > o2.getOrderIndex()){ return 1; }else if(o1.getOrderIndex() < o2.getOrderIndex()){ return -1; }else{ return 0; } } }
import java.util.Comparator; /** * 降序排序 * */ public class OrderDescStorComparator implements Comparator<Order> { @Override public int compare(Order o1, Order o2) { if(o1.getOrderIndex() > o2.getOrderIndex()){ return -1; }else if(o1.getOrderIndex() < o2.getOrderIndex()){ return 1; }else{ return 0; } } }
?当需要排序的时候,实现order这个接口,并实现相应的接口方法即可实现排序规则?