Java對象比較子對泛型List進行排序-Demo

來源:互聯網
上載者:User

標籤:

針對形如:
欄位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;

 

Java對象比較子對泛型List進行排序-Demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.