【java】实体类中按照特定的字段进行升序降序排序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
【java】实体类中按照特定的字段进⾏升序降序排序
背景:
实际页⾯上所有的分值都是按照JSON格式存储在⼀个字符串中存储在同⼀个字段中:
{"ownPTotal":"10>0","ownOTotal":"8>0","ownTotal1":"18","ownTotal2":"80","ownTotal3":"20","ownTotal4":"118","leadTotal1":"20","leadTotal2":"80","leadTotal3":"20","leadTotal4":"120","chiefCheck1":"","chiefCheck2":"","leadTotal5":"140"}
现在需要按照其中的⼏种分值进⾏升序/降序的排序操作
解决⽅法:
【因为不是按照原实体的中的字段进⾏排序,因此需要新建⼀个实体,将原实体中有⽤的字段和需要使⽤的值从字段中抽离出来在新实体中设置成字段,如果需要进⾏排序的字
段是原实体中就存在的,那就不需要新建实体了】
重点关注:
实现 Comparable接⼝需要实现的⽐较⽅法这⾥动态⽐较是按照 1.哪个字段进⾏排序 2.升序/降序
//排序⽅法
@Override
public int compareTo(NewMonthPerEntity o) {
double a1 = this.whichProperty.equals("leadTotal5") ? this.leadTotal5 : (this.whichProperty.equals("leadTotal4") ? this.leadTotal4 :(this.whichProperty.equals("ownTotal4") ? this.ownTotal4 :this.ownTotal4));
double a2 = o.whichProperty.equals("leadTotal5") ? o.leadTotal5 : (o.whichProperty.equals("leadTotal4") ? o.leadTotal4 :(o.whichProperty.equals("ownTotal4") ? o.ownTotal4 :o.ownTotal4));
if(this.ascDesc){//若是升序
return a1 > a2 ? 1 : -1;
}else{
return a1 > a2 ? -1 : 1;
}
}
【注意:这⾥ return a1 > a2 ? 1 : -1;并没有写成 return a1 > a2 ? 1 :(a1==a2 ? 0 :-1); 这个样⼦,是因为如果在⽐较的时候判定两个相等,那在放⼊TreeSet中时会发⽣覆盖现
象】
新实体完整代码:
1package com.agen.util;
2
3public class NewMonthPerEntity implements Comparable<NewMonthPerEntity>{
4
5private String monthPerId; //⽉度考核Id
6private String userId; //⽤户Id
7private String userName; //⽤户名称
8private String createDate; //考核创建时间
9private double leadTotal5; //调控分值
10private double leadTotal4; //考评分值
11private double ownTotal4; //⾃评分值
12private boolean ascDesc; //true升序/false降序
13private String whichProperty; //需要对哪个分值排序
14
15
16
17
18
19public String getMonthPerId() {
20return monthPerId;
21 }
22
23public void setMonthPerId(String monthPerId) {
24this.monthPerId = monthPerId;
25 }
26public String getUserId() {
27return userId;
28 }
29public void setUserId(String userId) {
erId = userId;
31 }
32public String getUserName() {
33return userName;
34 }
35public void setUserName(String userName) {
erName = userName;
37 }
38public String getCreateDate() {
39return createDate;
40 }
41
42public void setCreateDate(String createDate) {
43this.createDate = createDate;
44 }
45
46public double getLeadTotal5() {
47return leadTotal5;
48 }
49public void setLeadTotal5(double leadTotal5) {
50this.leadTotal5 = leadTotal5;
51 }
52public double getLeadTotal4() {
53return leadTotal4;
54 }
55public void setLeadTotal4(double leadTotal4) {
56this.leadTotal4 = leadTotal4;
57 }
58
59public double getOwnTotal4() {
60return ownTotal4;
61 }
62
63public void setOwnTotal4(double ownTotal4) {
64this.ownTotal4 = ownTotal4;
65 }
66public boolean isAscDesc() {
67return ascDesc;
68 }
69
70public void setAscDesc(boolean ascDesc) {
71this.ascDesc = ascDesc;
72 }
73
74public String getWhichProperty() {
75return whichProperty;
76 }
77public void setWhichProperty(String whichProperty) {
78this.whichProperty = whichProperty;
79 }
80
81public NewMonthPerEntity(String monthPerId, String userId, String userName,
82 String createDate, double leadTotal5, double leadTotal4,
83double ownTotal4) {
84super();
85this.monthPerId = monthPerId;
erId = userId;
erName = userName;
88this.createDate = createDate;
89this.leadTotal5 = leadTotal5;
90this.leadTotal4 = leadTotal4;
91this.ownTotal4 = ownTotal4;
92 }
93
94
95
96
97
98public NewMonthPerEntity(String monthPerId, String userId, String userName,
99 String createDate, double leadTotal5, double leadTotal4,
100double ownTotal4, boolean ascDesc, String whichProperty) {
101super();
102this.monthPerId = monthPerId;
erId = userId;
erName = userName;
105this.createDate = createDate;
106this.leadTotal5 = leadTotal5;
107this.leadTotal4 = leadTotal4;
108this.ownTotal4 = ownTotal4;
109this.ascDesc = ascDesc;
110this.whichProperty = whichProperty;
111 }
112
113
114
115
116
117
118//排序⽅法
119 @Override
120public int compareTo(NewMonthPerEntity o) {
121double a1 = this.whichProperty.equals("leadTotal5") ? this.leadTotal5 : (this.whichProperty.equals("leadTotal4") ? this.leadTotal4 :(this.whichProperty.equals("ownTotal4") ? this.ownTotal4 :this.ownTotal4)); 122double a2 = o.whichProperty.equals("leadTotal5") ? o.leadTotal5 : (o.whichProperty.equals("leadTotal4") ? o.leadTotal4 :(o.whichProperty.equals("ownTotal4") ? o.ownTotal4 :o.ownTotal4));
123if(this.ascDesc){//若是升序
124return a1 > a2 ? 1 : -1;
125 }else{
126return a1 > a2 ? -1 : 1;
127 }
128 }
129
130
131
132
133
134 }
View Code
@RequestMapping("allMonthPer")
@ResponseBody
public TreeSet<NewMonthPerEntity> allMonthPer(HttpServletRequest request,boolean ascDesc,String whichProperty) {
//-----------------部分代码---------------------------------------------------
.
.
.
.
//原实体得到的List
DetachedCriteria criteria = DetachedCriteria.forClass(Monthper.class);
criteria.add(Restrictions.in("userId", userIds));
List<Monthper> monthPers = monthperService.list(criteria);
//新建TreeSet 将原实体的List 对应的放置在新的实体中,放⼊TreeSet的时候就已经是按照传⼊【按照哪个字段排序/升序或降序】排好顺序的
TreeSet<NewMonthPerEntity> newSet = new TreeSet<NewMonthPerEntity>();
for (Monthper monthper : monthPers) {
String totalStr = monthper.getTotalStr();
double leadTotal5 = Double.parseDouble(JSONObject.fromObject(totalStr).getString("leadTotal5"));
double leadTotal4 = Double.parseDouble(JSONObject.fromObject(totalStr).getString("leadTotal4"));
double ownTotal4 = Double.parseDouble(JSONObject.fromObject(totalStr).getString("ownTotal4"));
NewMonthPerEntity entity = new NewMonthPerEntity(monthper.getMonthPerId(),monthper.getUserId(),userService.get(monthper.getUserId()).getUserName(),monthper.getCreateDate().toLocaleString(),leadTotal5,leadTotal4,ownTotal4); entity.setAscDesc(ascDesc);//设置升序/降序
if("".equals(whichProperty) || null == whichProperty){
entity.setWhichProperty("ownTotal4");
}else{
entity.setWhichProperty(whichProperty);
}
newSet.add(entity);
}
return newSet;
//在前台只需要$.each取出来追加到页⾯就好
}
效果如下图:
附带前台jsp代码
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2 <%@ taglib uri="/jsp/jstl/core" prefix="c" %>
3 <%
4String path = request.getContextPath();
5String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
6 %>
7
8<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
9<html>
10<head>
11<title>考核列表</title>
12
13<meta http-equiv="pragma" content="no-cache">
14<meta http-equiv="cache-control" content="no-cache">
15<meta http-equiv="expires" content="0">
16<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17
18<link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet">
19<script src="../js/jquery.js"></script>
20
21</head>
22
23<body>
24
25<div class="panel panel-primary">
26<div class="panel-heading">
27<div class="form-group">
28<label class="panel-title">⽉度绩效考评列表</label>
29
30<button type="button" class="btn btn-success thisMonth">本⽉考核</button>
31<button type="button" class="btn btn-warning userRefresh">刷新页⾯</button>
32<c:if test="${roleGrade==0 }">
33<div class="row">
34<div class="col-lg-2">
35<div class="input-group">
36<select class="form-control dep" name="departmentId">
37<option value="0">可选择所在部门</option>
38</select>
39</div>
40</div>
41<div class="col-lg-1">
42<div class="input-group">
43<input type="text" class="form-control us" placeholder="⽤户名称">
44</div>
45</div>
46<div class="col-lg-1">
47<div class="input-group">
48<input type="number" class=" form-control mon" placeholder="查询⽉份">
49</div>
50</div>
51<div class="col-lg-1">
52<div class="input-group">
53<button type="button" class="btn btn-default mohu ">模糊查询</button>
54</div>
55</div>
56</div>
57</c:if>
58
59</div>
60</div>
61<div class="panel-body">
62<input type="hidden" class="operationId" value="${operationId}"/>
63<input type="hidden" class="whichProperty"/>
64<input type="hidden" class="ascDesc"/>
65
66<table class="table text-center">
67<tr class="firstTr">
68<td class="active"><input type="checkbox" id="checkAll" name="checkAll"/></td>
69<td class="primary"><h4>考核⽉份</h4></td>
70<td class="success"><h4>员⼯名称</h4></td>
71<td class="danger"><h4>考评时间</h4></td>
72<td class="warning"><h4>调控分值<span class="glyphicon glyphicon-sort" alt="leadTotal5" style="float: right;"></span></h4></td>
73<td class="info"><h4>考评分值<span class="glyphicon glyphicon-sort" alt="leadTotal4" style="float: right;"></span></h4></td>
74<td class="danger"><h4>⾃评分值<span class="glyphicon glyphicon-sort" alt="ownTotal4" style="float: right;"></span></h4></td>
75<td class="default"><h4>操作</h4></td>
76</tr>
77</table>
78</div>
79</div>
80
81</body>
82
83<script src="../bootstrap/js/bootstrap.min.js"></script>
84<script src="../layer/layer.js"></script>
85<script type="text/javascript" src="../js/UUID.js"></script>
86<script type="text/javascript" src="../js/perContent/monthPerContent/allMonth.js"></script>
87
88</html>
View Code
附带前台js代码
1function refreshAll(ascDesc,whichProperty){
2 $("table tbody tr").not(":first").remove();
3 $.ajax({url:"../monthPerCon/allMonthPer.htmls",
4 dataType:'json',
5 type:"post",
6 data:{ascDesc:ascDesc,whichProperty:whichProperty},
7 traditional:false,
8 success:function(data){
9var temp ="";
10 $.each(data,function(i,d){
11 temp += '<tr>'
12 +' <td class="active"> '
13 +'<input type="checkbox" id="checkAll" name="checkAll" />'
14 +'<input type="hidden" name="monthPerId" value="'+d.monthPerId+'"/>'
15 +'</td>'
16 +'<td class="primary">'
17 +(new Date(d.createDate).getMonth()+"⽉考核")
18 +'</td>'
19 +'<td class="success">'
20 +'<input type="hidden" name="userId" value="'+erId+'"/>'
21 +erName
22 +'</td>'
23 +'<td class="danger">'
24 +d.createDate
25 +'</td>'
26 +'<td class="warning tips">'
27 +'【调控分值:'+d.leadTotal5+'】'
28 +'</td>'
29 +'<td class="info tips">'
30 +'【考评分值:'+d.leadTotal4+'】'
31 +'</td>'
32 +'<td class="danger tips">'
33 +'【⾃评分值:'+d.ownTotal4+'】'
34 +'</td>'
35 +'<td class="default">'
36 +'<a class="aClick"><i class="glyphicon glyphicon-list-alt"></i>详情</a> <a class="Adelete"><i class="glyphicon glyphicon-trash"></i>删除</a>'
37 +'</td>'
38 +'</tr>';
39 $(".whichProperty").val(d.whichProperty);
40 $(".ascDesc").val(d.ascDesc);
41 });
42 $(".firstTr").after(temp);
43
44
45/*//判断此时按那列排序升序降序改变图标
46 var whichProperty = $(".whichProperty").val();
47 var ascDesc = $(".ascDesc").val();
48 var thisSpan;
49 var cla = "";
50 whichProperty == "ownTotal4"? thisSpan = $("span").eq(2):(whichProperty == "leadTotal4"? thisSpan = $("span").eq(1) :thisSpan = $("span").eq(0));
51 ascDesc == "true" ? cla = "glyphicon glyphicon-sort-by-attributes" : cla="glyphicon glyphicon-sort-by-attributes-alt";
52 $(thisSpan).attr("class",cla);*/
53 }
54 });
55 }
56
57
58
59
60
61function mohu(ascDesc,whichProperty,dep,us,mon){
62
63 $("table tbody tr").not(":first").remove();
64 $.ajax({url:"../monthPerCon/mohuAllMonthPer.htmls",
65 dataType:'json',
66 type:"post",
67 data:{ascDesc:ascDesc,whichProperty:whichProperty,departmentId:dep,userName:us,month:mon},
68 traditional:false,
69 success:function(data){
70var temp ="";
71 $.each(data,function(i,d){
72 temp += '<tr>'
73 +' <td class="active"> '
74 +'<input type="checkbox" id="checkAll" name="checkAll" />'
75 +'<input type="hidden" name="monthPerId" value="'+d.monthPerId+'"/>'
76 +'</td>'
77 +'<td class="primary">'
78 +(new Date(d.createDate).getMonth()+"⽉考核")
79 +'</td>'
80 +'<td class="success">'
81 +'<input type="hidden" name="userId" value="'+erId+'"/>'
82 +erName
83 +'</td>'
84 +'<td class="danger">'
85 +d.createDate
86 +'</td>'
87 +'<td class="warning tips">'
88 +'【调控分值:'+d.leadTotal5+'】'
89 +'</td>'
90 +'<td class="info tips">'
91 +'【考评分值:'+d.leadTotal4+'】'
92 +'</td>'
93 +'<td class="danger tips">'
94 +'【⾃评分值:'+d.ownTotal4+'】'
95 +'</td>'
96 +'<td class="default">'
97 +'<a class="aClick"><i class="glyphicon glyphicon-list-alt"></i>详情</a> <a class="Adelete"><i class="glyphicon glyphicon-trash"></i>删除</a>'
98 +'</td>'
99 +'</tr>';
100 });
101 $(".firstTr").after(temp);
102
103 }
104 });
105
106 }
107
108
109
110
111
112 $(document).ready(function(){
113 refreshAll();
114
115//升序降序
116 $("span").click(function(){
117
118var whichProperty = $(this).attr("alt");
119if($(this).attr("class") == "glyphicon glyphicon-sort"){
120 $(this).attr("class","glyphicon glyphicon-sort-by-attributes-alt");
121 }else if($(this).attr("class") == "glyphicon glyphicon-sort-by-attributes-alt"){
122 $(this).attr("class","glyphicon glyphicon-sort-by-attributes");
123 }else if($(this).attr("class") == "glyphicon glyphicon-sort-by-attributes"){
124 $(this).attr("class","glyphicon glyphicon-sort-by-attributes-alt");
125 }
126
127 $("span").not(this).attr("class","glyphicon glyphicon-sort");
128var ascDesc = $(this).attr("class") == "glyphicon glyphicon-sort" ? false : ($(this).attr("class") == "glyphicon glyphicon-sort-by-attributes-alt" ? false : true);
129
130//判断是否模糊查询
131var dep = $(".dep option:selected").val();
132var us = $(".us").val();
133var mon = $(".mon").val();
134if(dep !="" || us !="" || mon !=""){//只要模糊查询有值
135if(( parseInt(mon)<0 || parseInt(mon)>12 )){
136 layer.msg("⽉份不正确",{icon:2,time:1000});
137 $(".mon").val("");
138 }else{
139 mohu(ascDesc,whichProperty,dep,us,mon);
140 }
141 }else{//否则查询
142 refreshAll(ascDesc,whichProperty);
143 }
144
145 });
146
147
148
149 $(document).on("click",".aClick",function(){
150var monthPerId = $(this).parents("tr").children().first().find("input:hidden").val();
151 location.href = "../monthPerCon/showThisMonper.htmls?monthPerId="+monthPerId;
152 });
153 $(document).on("click",".Adelete",function(){
154var operationId = $(".operationId").val();//操作⼈的ID
155var userId = $(this).parents("tr").children().eq(2).find(":hidden").val();//本条考核的⼈员
156if(operationId != userId){
157 layer.msg("⾮本⼈⽆法删除!",{icon:2,time:2000});
158 }else{
159var monthPerId = $(this).parents("tr").children().first().find("input:hidden").val();
160var leadTotal4 = JSON.parse($(this).parents("tr").children().eq(4).find(":hidden").val()).leadTotal4;
161if( !(leadTotal4 == undefined || leadTotal4=="") ){
162 layer.msg("经理已经考评⽆法删除!");
163 }else{
164 $.ajax({url:"../monthPerCon/deleteThisMonper.htmls",
165 dataType:'json',
166 type:"post",
167 data:{monthPerId:monthPerId},
168 traditional:false,
169 success:function(data){
170if(data){
171 layer.msg("删除成功",{icon:1,time:2000},function(){
172 refreshAll();
173 });
174 }else{
175 layer.msg("删除失败",{icon:2,time:2000},function(){
176 refreshAll();
177 });
178 }
179
180 }
181 });
182 }
183 }
184
185// location.href = "../monthPerCon/deleteThisMonper.htmls?monthPerId="+monthPerId;
186 });
187
188//跳转到本⽉考核页⾯
189 $(".thisMonth").click(function(){
190 location.href = "../monthPerCon/perContent.htmls";
191 });
192
193
194//部门
195 $.getJSON("../department/showAlldepartment.htmls", function(data){
196if(data!=null){
197var temp = "";
198 $.each(data,function(i,a){
199 temp+="<option value='"+a.departmentId+"'>"+a.departmentName+"</option>";
200 });
201 $('.dep option').first().after(temp);
202 }
203 });
204
205
206//模糊查询
207 $(".mohu").click(function(){
208var dep = $(".dep option:selected").val();
209var us = $(".us").val();
210var mon = $(".mon").val();
211if(( parseInt(mon)<0 || parseInt(mon)>12 )){ 212 layer.msg("⽉份不正确",{icon:2,time:1000}); 213 $(".mon").val("");
214 }else{
215 mohu(false,"ownTotal4",dep,us,mon);
216 }
217
218 });
219
220
221 });
View Code。