Java中的List按照元素的属性进行排序

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

 

1. JavaBean —— Content.java:

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:

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.     }  
  26. }  

 

 

 

3. 测试类 —— Test Class —— CompareClient.java

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值