例如:list=[{COUNTTICKET=1, PRECOUNTOUT=2}, {COUNTTICKET=6, PRECOUNTOUT=7}, {COUNTTICKET=7, PRECOUNTOUT=7}, {COUNTTICKET=1, PRECOUNTOUT=1}]
多个字段中的某个字段的排序:
import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.Comparator;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
public
class
$ {
public
static
void
main(String[] args) {
List
new
ArrayList
init(data);
System.out.println(
"排序前:"
);
System.out.println(data);
sort(data);
System.out.println(
"排序后:"
);
System.out.println(data);
}
private
static
void
sort(List
Collections.sort(data,
new
Comparator
public
int
compare(Map o1, Map o2) {
Integer a = (Integer) o1.get(
"PRECOUNTOUT"
);
Integer b = (Integer) o2.get(
"PRECOUNTOUT"
);
// 升序
return
a.compareTo(b);
// 降序
// return b.compareTo(a);
}
});
}
private
static
void
init(List
Map map =
new
HashMap();
map.put(
"COUNTTICKET"
,
1
);
map.put(
"PRECOUNTOUT"
,
2
);
data.add(map);
map =
new
HashMap();
map.put(
"COUNTTICKET"
,
6
);
map.put(
"PRECOUNTOUT"
,
7
);
data.add(map);
map =
new
HashMap();
map.put(
"COUNTTICKET"
,
8
);
map.put(
"PRECOUNTOUT"
,
5
);
data.add(map);
map =
new
HashMap();
map.put(
"COUNTTICKET"
,
2
);
map.put(
"PRECOUNTOUT"
,
3
);
data.add(map);
}
}