給java中的List進行排序

來源:互聯網
上載者:User

主要用到: Collections.sort()方法:

 

1. JavaBean —— Content.java:

  1. package com.hmw.listsort;   
  2.   
  3. public class Content {   
  4.   
  5.     private long key;   
  6.   
  7.     private String name;   
  8.   
  9.     public Content(long key, String name) {   
  10.         this.key = key;   
  11.         this.name = name;   
  12.     }   
  13.   
  14.     public long getKey() {   
  15.         return key;   
  16.     }   
  17.   
  18.     public void setKey(long key) {   
  19.         this.key = key;   
  20.     }   
  21.   
  22.     public String getName() {   
  23.         return name;   
  24.     }   
  25.   
  26.     public void setName(String name) {   
  27.         this.name = name;   
  28.     }   
  29. }  

2. 進行比較的類 —— ContentComparator.java:

  1. package com.hmw.listsort;   
  2.   
  3. import java.util.Comparator;   
  4.   
  5. public class ContentComparator implements Comparator<Content>{   
  6.     public int compare(Content o1, Content o2) {   
  7.   
  8.         //將 null 排到最後   
  9.         if(o1 == null){   
  10.             return 1;   
  11.         }   
  12.         if(o2 == null || !(o2 instanceof Content)){   
  13.             return -1;   
  14.         }   
  15.            
  16.         long key1 = o1.getKey();   
  17.         long key2 = o2.getKey();   
  18.         return key1 > key2 ? 1 : key1 < key2 ? -1 : 0;   
  19.            
  20.         /*  
  21.         //如果想要按照 name 欄位進行排序, 只需將最後三行代碼改為下面這一行即可  
  22.         return o1.getName().compareTo(o2.getName());  
  23.         */
  24.     }   
  25. }  

 

 

 3. 測試類別 —— CompareClient.java

  1. package com.hmw.listsort;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.Collections;   
  5. import java.util.List;   
  6.   
  7. public class CompareClient {   
  8.     public static void main(String[] args) {   
  9.         List<Content> list = new ArrayList<Content>();   
  10.         list.add(null);   
  11.         list.add(new Content(15000, "---15000--"));   
  12.         list.add(new Content(10000, "---10000---"));   
  13.         list.add(new Content(20000, "---20000---"));   
  14.         list.add(null);   
  15.         list.add(new Content(25000, "---25000---"));   
  16.         list.add(new Content(13000, "---13000---"));   
  17.         list.add(new Content(15000, "---15000---"));   
  18.         list.add(new Content(89000, "---89000---"));   
  19.         Collections.sort(list, new ContentComparator());   
  20.            
  21.         /*  
  22.         //如果要按照降序排列再加上這一行代碼即可  
  23.         Collections.reverse(list);  
  24.         */  
  25.            
  26.         for (Content content : list) {   
  27.             if (content == null) {   
  28.                 System.out.println("null");   
  29.             } else {   
  30.                 System.out.println("content.getName()/t" + content.getName());   
  31.             }   
  32.         }   
  33.     }   
  34. }  

 

輸出結果如下:

content.getName() ---10000---
content.getName() ---13000---
content.getName() ---15000--
content.getName() ---15000---
content.getName() ---20000---
content.getName() ---25000---
content.getName() ---89000---
null
null

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.