1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package
com.hanchao.test;
/**
* 实体类Step
* @author liweihan (liweihan@sohu-inc.com)
* @version 1.0 (2016年1月13日 下午4:30:59)
*/
public
class
Step {
/** 处理时间 */
private
String acceptTime;
/** 快件所在地点*/
private
String acceptAddress;
public
Step() {
super
();
}
public
Step(String acceptTime,String acceptAddress) {
super
();
this
.acceptTime = acceptTime;
this
.acceptAddress = acceptAddress;
}
public
String getAcceptTime() {
return
acceptTime;
}
public
void
setAcceptTime(String acceptTime) {
this
.acceptTime = acceptTime;
}
public
String getAcceptAddress() {
return
acceptAddress;
}
public
void
setAcceptAddress(String acceptAddress) {
this
.acceptAddress = acceptAddress;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package
com.hanchao.test;
import
java.text.ParseException;
import
java.text.SimpleDateFormat;
import
java.util.Comparator;
import
java.util.Date;
/**
* 对Step类排序的接口
* @author liweihan (liweihan@sohu-inc.com)
* @version 1.0 (2016年1月13日 下午4:35:26)
*/
public
class
StepComparator
implements
Comparator<Step>{
private
static
final
SimpleDateFormat yyyyMMdd =
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
);
/**
* 如果o1小于o2,返回一个负数。
* 如果o1大于o2,返回一个正数。
* 如果o1等于o2,则返回0
*/
@Override
public
int
compare(Step o1, Step o2) {
try
{
Date acceptTime1 = yyyyMMdd.parse(o1.getAcceptTime());
Date acceptTime2 = yyyyMMdd.parse(o2.getAcceptTime());
//对字段进行升序,如果要降序可以采用before方法
if
(acceptTime1.after(acceptTime2)) {
return
1
;
}
}
catch
(ParseException e) {
e.printStackTrace();
}
return
-
1
;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
package
com.hanchao.test;
import
java.text.ParseException;
import
java.text.SimpleDateFormat;
import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.Comparator;
import
java.util.Date;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
java.util.TreeMap;
import
com.sohu.star.pojo.Album;
/**
* @author liweihan (liweihan@sohu-inc.com)
* @version 1.0 (2016年1月13日 下午4:47:49)
*/
public
class
StepComparatorTest {
private
static
final
SimpleDateFormat yyyyMMdd =
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
);
public
static
void
main(String[] args) {
List<Step> list =
new
ArrayList<Step>();
Step step1 =
new
Step();
step1.setAcceptAddress(
"step1"
);
step1.setAcceptTime(
"2015-12-11 00:00:00"
);
Step step2 =
new
Step();
step2.setAcceptAddress(
"step2"
);
step2.setAcceptTime(
"2016-01-10 00:00:00"
);
Step step3 =
new
Step();
step3.setAcceptAddress(
"step3"
);
step3.setAcceptTime(
"2013-12-12 00:00:00"
);
list.add(step1);
list.add(step2);
list.add(step3);
//对集合对象进行排序
/* StepComparator comparator = new StepComparator();
Collections.sort(list,comparator);
for (Step step : list) {
System.out.println(step.getAcceptAddress());
}*/
//结果(升序):step3 step1 step2
/**
* 当然我们还可以简化掉StepComparator类
*/
Collections.sort(list,new Comparator<Step>() {
@Override
public int compare(Step o1, Step o2) {
try {
Date acceptTime1 = yyyyMMdd.parse(o1.getAcceptTime());
Date acceptTime2 = yyyyMMdd.parse(o2.getAcceptTime());
//对字段进行降序,如果要升序可以采用after方法
if (acceptTime1.before(acceptTime2)) {
return 1;
}
} catch (ParseException e) {
e.printStackTrace();
}
return -1;
}
});
for (Step step : list) {
System.out.println(step.getAcceptAddress());
}
//结果(降序):step2 step1 step3
/**
* 我们对map集合做一下排序,每次添加后对map集合进行自动排序!
* 如果o1小于o2,返回一个负数。
* 如果o1大于o2,返回一个正数。
* 如果o1等于o2,则返回0
*/
Map<Integer,String> map =
new
HashMap<Integer, String>();
map =
new
TreeMap<Integer, String>(
new
Comparator<Integer>() {
@Override
public
int
compare(Integer o1, Integer o2) {
// return o2 - o1;//倒叙
// return o1 - o2;//正序
// return o1 - o2 > 0 ? -1 : 1;//倒序
return
o1 - o2 >
0
?
1
: -
1
;
//正序
}
});
map.put(
5
,
"5"
);
map.put(
3
,
"3"
);
map.put(
2
,
"2"
);
map.put(
1
,
"1"
);
map.put(
4
,
"4"
);
System.out.println(map);
//结果:{1=1, 2=2, 3=3, 4=4, 5=5}
}
}
|
本文转自韩立伟 51CTO博客,原文链接:,http://blog.51cto.com/hanchaohan/1734709
如需转载请自行联系原作者