一、comparator 接口继承
public class ComparatorTest { /** * @param args */ public static void main(String[] args) { // ----写程序 List<Step> list=new ArrayList<Step>(); for (int i = 10; i < 12; i++) { list.add(new Step(i)); } for (int i = 5; i < 9; i++) { list.add(new Step(i)); } Comparators com=new Comparators(); Collections.sort(list,com); System.out.println(list); } } class Step{ int datatime; Step(int i){ datatime=i; } public int getDatatime() { return datatime; } @Override public String toString() { // TODO Auto-generated method stub return "Step("+datatime+")"; } } class Comparators implements Comparator<Step>{ public int compare(Step o1, Step o2) { // TODO Auto-generated method stub return o1.getDatatime()>o2.getDatatime()?1:o1.getDatatime()==o2.getDatatime()?0:-1; } }
时间: 2024-10-31 17:23:46