ValueComparator.java 981 B

123456789101112131415161718192021222324252627282930313233
  1. package com.yihu.wlyy.util;
  2. import org.json.JSONObject;
  3. import java.util.Comparator;
  4. import java.util.Map;
  5. import java.util.jar.JarEntry;
  6. /**
  7. * Created by Trick on 2017/5/24.
  8. */
  9. public class ValueComparator implements Comparator<String> {
  10. Map<String, JSONObject> base;
  11. //这里需要将要比较的map集合传进来
  12. public ValueComparator(Map<String, JSONObject> base) {
  13. this.base = base;
  14. }
  15. // Note: this comparator imposes orderings that are inconsistent with equals.
  16. //比较的时候,传入的两个参数应该是map的两个key,根据上面传入的要比较的集合base,可以获取到key对应的value,然后按照value进行比较
  17. public int compare(String a, String b) {
  18. JSONObject j1 = base.get(a);
  19. JSONObject j2 = base.get(b);
  20. j1.get("amount");
  21. j2.get("amount");
  22. if((Long)j1.get("amount")>=(Long)j2.get("amount")){
  23. return -1;
  24. }
  25. return 1;
  26. }
  27. }