有关Java list排序的解析
创始人
2024-04-01 14:21:34
0

此处Java list 排序主要用到Collections.sort方法

  1. package com.tom.compare;  
  2.  
  3. import java.util.ArrayList;  
  4.  
  5. import java.util.Collections;  
  6.  
  7. import java.util.Comparator;  
  8.  
  9. import java.util.List;  
  10.  
  11. public class CompareClient {  
  12.  
  13.  /**  
  14.  
  15.   * @param args  
  16.  
  17.   */ 
  18.  
  19.  public static void main(String[] args) {  
  20.  
  21.   // TODO Auto-generated method stub  
  22.  
  23.   List list = new ArrayList();  
  24.  
  25.   list.add(new Content(15000,"1asdfasd5000"));  
  26.  
  27.   list.add(new Content(10000,"10000"));  
  28.  
  29.   list.add(new Content(20000,"20000"));  
  30.  
  31.   list.add(new Content(30000,"30000"));  
  32.  
  33.   list.add(new Content(25000,"25000"));  
  34.  
  35.   list.add(new Content(13000,"13000"));  
  36.  
  37.   list.add(new Content(15000,"15000"));    
  38.  
  39.   list.add(new Content(89000,"89000"));  
  40.  
  41.     
  42.  
  43.   ContentComparator comp = new ContentComparator();    
  44.  
  45.   Collections.sort(list,comp);  
  46.  
  47.     
  48.  
  49.   Content content;  
  50.  
  51.   for(int i = 0; i < list.size(); i++){  
  52.  
  53.    content = (Content)list.get(i);  
  54.  
  55.    System.out.println(" content.getName() " + content.getName());  
  56.  
  57.   }  
  58.  
  59.  }  
  60.  
  61. }  
  62.  
  63. package com.tom.compare;  
  64.  
  65. import java.util.Comparator;  
  66.  
  67. public class ContentComparator implements Comparator {  
  68.  
  69.  public int compare(Object o1, Object o2) {  
  70.  
  71.   // TODO Auto-generated method stub  
  72.  
  73.   Content c1 = (Content) o1;  
  74.  
  75.   Content c2 = (Content) o2;  
  76.  
  77.   if (c1.getKey() > c2.getKey()) {  
  78.  
  79.    return 1;  
  80.  
  81.   } else {  
  82.  
  83.    if (c1.getKey() == c2.getKey()) {  
  84.  
  85.     return 0;  
  86.  
  87.    } else {  
  88.  
  89.     return -1;  
  90.  
  91.    }  
  92.  
  93.   }  
  94.  
  95.  }  
  96.  
  97. }  
  98.  
  99. package com.tom.compare;  
  100.  
  101. public class Content {  
  102.  
  103.  private long key;  
  104.  
  105.  private String name;  
  106.  
  107.  public Content(long key, String name) {  
  108.  
  109.   this.key = key;  
  110.  
  111.   this.name = name;  
  112.  
  113.  }  
  114.  
  115.  public long getKey() {  
  116.  
  117.   return key;  
  118.  
  119.  }  
  120.  
  121.  public void setKey(long key) {  
  122.  
  123.   this.key = key;  
  124.  
  125.  }  
  126.  
  127.  public String getName() {  
  128.  
  129.   return name;  
  130.  
  131.  }  
  132.  
  133.  public void setName(String name) {  
  134.  
  135.   this.name = name;  
  136.  
  137.  }  
  138.  
  139. }  

结果是:

  1. content.getName() 10000 
  2.  
  3. content.getName() 13000 
  4.  
  5. content.getName() 1asdfasd5000  
  6.  
  7. content.getName() 15000 
  8.  
  9. content.getName() 20000 
  10.  
  11. content.getName() 25000 
  12.  
  13. content.getName() 30000 
  14.  
  15. content.getName() 89000 

 

以下为按时间排序:

 

  1. public   static   void   sss()   {     
  2.              String[]   dates   =   {     
  3.                    "2   Dec   2003   12:12:05",     
  4.                    "2   Apr   2003   13:12:05",     
  5.                    "2   Jan   2003   10:12:05",     
  6.                    "2   Feb   2003   15:12:05",     
  7.              };     
  8.              java.text.SimpleDateFormat   f   =   new   java.text.SimpleDateFormat("d   MMM   y   HH:mm:ss",Locale.ENGLISH);     
  9.              try{     
  10.                    System.out.println("before:");     
  11.                    for(int   i   =   0;   i                         System.out.println(dates[i]);     
  12.                    }     
  13.      
  14.                    Arrays.sort(dates,   new   Comparator()   {     
  15.                          public   int   compare(Object   o1,   Object   o2){     
  16.                                try{     
  17.                                      SimpleDateFormat   df   =   new   java.text.SimpleDateFormat("d   MMM   y   HH:mm:ss",Locale.ENGLISH);     
  18.                                      Date   d1   =   df.parse((String)o1);     
  19.                                      Date   d2   =   df.parse((String)o2);     
  20.                                      return   d1.compareTo(d2);     
  21.                                      }catch(Exception   e){e.printStackTrace();}     
  22.                                      return   -1;     
  23.                          }     
  24.                    });     
  25.                    System.out.println("after:");     
  26.                    for(int   i   =   0;   i                         System.out.println(dates[i]);     
  27.                    }     
  28.              }catch(Exception   e){e.printStackTrace();}     
  29.        }    

 

 

【编辑推荐】

  1. 20个开发人员非常有用的Java功能代码
  2. 走进Java 7中的模块系统
  3. JavaFX 1.2 已经发布 主要新功能一览
  4. 2009年十大Java技术解决方案
  5. 2008最值得学习的五种JAVA技术

 

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...