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);

 

相关文章:

  • 2021-11-28
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
  • 2021-09-24
  • 2021-10-26
  • 2021-11-29
猜你喜欢
  • 2021-12-17
  • 2022-12-23
  • 2021-07-30
  • 2022-12-23
  • 2022-12-23
  • 2021-07-14
  • 2022-12-23
相关资源
相似解决方案