自定义(复杂)类型,如果遇到按照不同的规则进行排序,可以参照以下的方法。
参考资料:http://z-jianwen.iteye.com/blog/616316
在此,举一例:
package com; public class Goods { // 定义全参构造器 public Goods(int _num,int _weigth,int _value){ this.setNum(_num); this.setValue(_value); this.setWeigth(_weigth); } // 定义方法,获得单位价值 public int getPervalue(){ int perValue = this.value/this.weigth; return perValue; } @Override public String toString() { return "Goods[" + this.getNum() + "] : weight = " + this.getWeigth() + " value = " + this.getValue(); } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public int getWeigth() { return weigth; } public void setWeigth(int weigth) { this.weigth = weigth; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } // 属性 private int num; private int weigth; private int value; }
package comparator; import java.util.Comparator; import com.Goods; public class PerValueComparator implements Comparator<Goods> { @Override public int compare(Goods o1, Goods o2) { // 单位价值大的排前面 if (o1.getPervalue() != o2.getPervalue()) { return o2.getPervalue() - o1.getPervalue(); }else { // 如果两者的单位价值相等,则重量小的排前面 return o1.getWeigth() - o2.getWeigth(); } } }
package comparator; import java.util.Comparator; import com.Goods; public class ValueComparator implements Comparator<Goods> { @Override public int compare(Goods o1, Goods o2) { // 价值大的排前面 if (o1.getValue() != o2.getValue()) { return o2.getValue() - o1.getValue(); }else { // 如果两者的价值相等,则重量小的排前面 return o1.getWeigth() - o2.getWeigth(); } } }
package comparator; import java.util.Comparator; import com.Goods; public class WeightComparator implements Comparator<Goods> { @Override public int compare(Goods o1, Goods o2) { // 重量小的排前面 if (o1.getWeigth() != o2.getWeigth()) { return o1.getWeigth() - o2.getWeigth(); }else { // 如果两者的重量相等,则价值大的排前面 return o2.getValue() - o1.getValue(); } } }
package test; import java.util.ArrayList; import java.util.Collections; import com.Goods; import comparator.PerValueComparator; import comparator.ValueComparator; import comparator.WeightComparator; public class TestCompare { public static void main(String[] args) { ArrayList<Goods> list = new ArrayList<Goods>(); list.add(new Goods(1,20,30)); list.add(new Goods(2,8,10)); list.add(new Goods(3,10,15)); list.add(new Goods(4,30,35)); list.add(new Goods(5,5,8)); list.add(new Goods(6,30,45)); list.add(new Goods(7,18,10)); list.add(new Goods(8,4,5)); System.out.println("---------- before sort ----------"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } // 根据重量从小到大排列 Collections.sort(list, new WeightComparator()); System.out.println("---------- 根据重量从小到大排列 ----------"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } // 根据价值从大到小排列 Collections.sort(list, new ValueComparator()); System.out.println("---------- 根据价值从大到小排列 ----------"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } // 根据单位价值从大到小排列 Collections.sort(list, new PerValueComparator()); System.out.println("---------- 根据单位价值从大到小排列 ----------"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }
---------- before sort ---------- Goods[1] : weight = 20 value = 30 Goods[2] : weight = 8 value = 10 Goods[3] : weight = 10 value = 15 Goods[4] : weight = 30 value = 35 Goods[5] : weight = 5 value = 8 Goods[6] : weight = 30 value = 45 Goods[7] : weight = 18 value = 10 Goods[8] : weight = 4 value = 5 ---------- 根据重量从小到大排列 ---------- Goods[8] : weight = 4 value = 5 Goods[5] : weight = 5 value = 8 Goods[2] : weight = 8 value = 10 Goods[3] : weight = 10 value = 15 Goods[7] : weight = 18 value = 10 Goods[1] : weight = 20 value = 30 Goods[6] : weight = 30 value = 45 Goods[4] : weight = 30 value = 35 ---------- 根据价值从大到小排列 ---------- Goods[6] : weight = 30 value = 45 Goods[4] : weight = 30 value = 35 Goods[1] : weight = 20 value = 30 Goods[3] : weight = 10 value = 15 Goods[2] : weight = 8 value = 10 Goods[7] : weight = 18 value = 10 Goods[5] : weight = 5 value = 8 Goods[8] : weight = 4 value = 5 ---------- 根据单位价值从大到小排列 ---------- Goods[8] : weight = 4 value = 5 Goods[5] : weight = 5 value = 8 Goods[2] : weight = 8 value = 10 Goods[3] : weight = 10 value = 15 Goods[1] : weight = 20 value = 30 Goods[6] : weight = 30 value = 45 Goods[4] : weight = 30 value = 35 Goods[7] : weight = 18 value = 10