Collections.sort多字段排序小節

Collections.sort(result, new Comparator<Map<String, Object>>() {
			@Override
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				//先按順序排序
				if (Integer.parseInt(o1.get("itemorder").toString())-(Integer.parseInt(o2.get("itemorder").toString()))>0) {
					return 1;
				} else if (Double.parseDouble(o1.get("price").toString())-(Double.parseDouble(o2.get("price").toString()))<0) {
					return -1;
				} else if (Double.parseDouble(o1.get("price").toString())-(Double.parseDouble(o2.get("price").toString()))==0) {
					//順序相等的才需要第二次排序
					if (Double.parseDouble(o1.get("price").toString())-(Double.parseDouble(o2.get("price").toString()))>0) {
						return 1;
					} else if (Double.parseDouble(o1.get("price").toString())-(Double.parseDouble(o2.get("price").toString()))<0) {
						return -1;
					}
				}
				return 0;
			}
		});

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章