java类排序
1、实现Comparator接口
public static class ComparatorImpl implements Comparator<Element>{ @Override public int compare(Element o1, Element o2) { if(o1.unitPrice > o2.unitPrice) return 1; else if(o1.unitPrice < o2.unitPrice){ return -1; }else{ return 0; } } }
public static class Element {
public int distance;
public int fare;
public float unitPrice;
}
2、调用Collections的sort方法排序
ArrayList<Element> data = new ArrayList<Element>(); data.add(...); //sort the data - small to big ComparatorImpl comp = new ComparatorImpl(); Collections.sort(data, comp);