标签:
针对形如:
字段1 字段2 字段3 字段n
1 hello 26 789
1 world 89 556
2 what 55 456
2 the 85 45
2 fuck 55 99
5 haha 98 455
以上类型的查询数据结果,需要对 字段3 进行求和分组(在SQL查询无法一次性完成的情况下,通常采用Java分组排序),大概思路如下:
1、在Bean中添加相关的分组标记字段,对求和或者其它统计的结果进行插入分组标记,下面demo中为bigIdOrder标记
2、对完成标记的List进行标记的补全
3、对补全的List按照标记进行排序
PS:非常不赞成在前端用JS/JQ等进行排序,非常费劲,并且数组还会发生不可知的变化,笔者吃过亏(算法高手除外),笔者编码水平有限,写的不到位的地方恳请大神们批评指正。
因编码直接来源于项目,变量名未做统一demo化,现给出提示:
bigId:字段1
pinci:字段3
bigIdOrder:标记字段(可自定义)
JSONObject json = new JSONObject(); String dataType = request.getParameter("dataType"); String channelId = request.getParameter("channelId"); String order = request.getParameter("type"); setMapTimeRight(dataType, 0); map.put("channelId", channelId); map.put("order", order); try { List<UnconventionBrandAnalyseByBrand> now = this.service .unconventionBrandSituation(map); //1、产生组装排序条件字段数据标记 int bigIdOrder=0; int i=-1; int i2=0; List bigIdOrderArray = new ArrayList(); for(UnconventionBrandAnalyseByBrand un : now){ i++; //如果当前bigId与前一个bigId相等,则将该id视为一个id(bigIdOrder),对该id所对应的所有频次数进行累加 if(i==0 &&now.get(0).getBigId().equals(now.get(1).getBigId())){ //第一次计数后有(非分组条件) bigIdOrder = bigIdOrder+Integer.parseInt(now.get(i).getPinci()); }else if(i>0 &&i<now.size()-1 &&!now.get(i).getBigId().equals(now.get(i-1).getBigId()) &&now.get(i).getBigId().equals(now.get(i+1).getBigId())){ //前不同后相同(非分组条件) bigIdOrder = bigIdOrder+Integer.parseInt(now.get(i).getPinci()); }else if(i>0 &&i<now.size()-1 &&now.get(i).getBigId().equals(now.get(i+1).getBigId()) &&now.get(i).getBigId().equals(now.get(i+1).getBigId())){ //前相同后相同(非分组条件) bigIdOrder = bigIdOrder+Integer.parseInt(now.get(i).getPinci()); }else{ //分组条件 bigIdOrder = bigIdOrder+Integer.parseInt(now.get(i).getPinci()); un.setBigIdOrder(bigIdOrder+"");//添加分组标识(int类型bigIdOrder) bigIdOrderArray.add(i2, bigIdOrder+""); i2++; bigIdOrder = 0; } } // 2、补全产生组装排序条件字段数据标记 int ii = 0; int ng = 0;// 非分组条件计数器 for (UnconventionBrandAnalyseByBrand un : now) { // 如果当前bigId与前一个bigId相等,则将该id视为一个id(bigIdOrder),对该id所对应的所有频次数进行累加 if (ii == 0 && !now.get(0).getBigId().equals(now.get(1).getBigId())) { // 第一次计数后无(分组条件) } else if (ii > 0 && i < now.size() - 1 && now.get(ii).getBigId().equals( now.get(ii - 1).getBigId()) && !now.get(ii).getBigId().equals( now.get(ii + 1).getBigId())) { // 前同后不同(分组条件) } else if (ii == now.size() - 1 && now.get(ii).getBigId().equals( now.get(ii - 1).getBigId())) { // 后无前同(分组条件) } else if (ii == now.size() - 1 && !now.get(ii).getBigId().equals( now.get(ii - 1).getBigId())) { // 后无前不同(分组条件) } else { // 非分组条件 if (ii == 0) { un.setBigIdOrder(bigIdOrderArray.get(0).toString()); } else if (!now.get(ii).getBigId().equals( now.get(ii - 1).getBigId())) { ng++; un.setBigIdOrder(bigIdOrderArray.get(ng).toString()); } else { un.setBigIdOrder(bigIdOrderArray.get(ng).toString()); } } ii++; } // 3、开始排序采用对象比较器 Comparator<UnconventionBrandAnalyseByBrand> comparator = new Comparator<UnconventionBrandAnalyseByBrand>() { public int compare(UnconventionBrandAnalyseByBrand s1, UnconventionBrandAnalyseByBrand s2) { // 排序标记 int ret = 0; if (s1.getBigIdOrder() != s2.getBigIdOrder()) { ret = Integer.parseInt(s1.getBigIdOrder()) - Integer.parseInt(s2.getBigIdOrder()); } return ret > 0 ? -1 : 1;// 升序或者降序改变-1和1的位置即可 } }; //开始自动排序 Collections.sort(now,comparator); json.put("data", now); try { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.print(json); out.close(); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } return null;
标签:
原文地址:http://www.cnblogs.com/dreamzhiya/p/4655533.html