有关Java list排序的解析

开发 后端
本文主要讲述Java list排序的相关知识,用到的排序方法是Collections.sort,并给出了相关的代码。

此处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<dates.length;   i++)   {     
  12.                          System.out.println(dates[i]);     
  13.                    }     
  14.      
  15.                    Arrays.sort(dates,   new   Comparator()   {     
  16.                          public   int   compare(Object   o1,   Object   o2){     
  17.                                try{     
  18.                                      SimpleDateFormat   df   =   new   java.text.SimpleDateFormat("d   MMM   y   HH:mm:ss",Locale.ENGLISH);     
  19.                                      Date   d1   =   df.parse((String)o1);     
  20.                                      Date   d2   =   df.parse((String)o2);     
  21.                                      return   d1.compareTo(d2);     
  22.                                      }catch(Exception   e){e.printStackTrace();}     
  23.                                      return   -1;     
  24.                          }     
  25.                    });     
  26.                    System.out.println("after:");     
  27.                    for(int   i   =   0;   i<dates.length;   i++)   {     
  28.                          System.out.println(dates[i]);     
  29.                    }     
  30.              }catch(Exception   e){e.printStackTrace();}     
  31.        }    

 

 

【编辑推荐】

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

 

责任编辑:仲衡 来源: gnrwaxlfa的博客
相关推荐

2010-02-05 16:44:27

Android平台

2016-12-30 13:59:44

Java 8比较器List 排序

2021-12-20 07:11:26

Java List排序 Java 基础

2010-02-06 09:38:42

Android调用服务

2023-09-26 22:22:30

选择排序Python

2010-02-05 17:55:01

谷歌Android操作

2010-03-05 13:46:12

Android编程学习

2010-01-28 13:15:43

C++参数

2010-02-03 17:52:11

Python 2.0

2010-02-05 18:04:36

Android程序框架

2009-06-11 17:03:29

Java线程

2009-09-14 18:34:32

C# List排序

2009-06-11 13:00:08

Java数组赋值

2023-02-28 17:24:32

顺串字符串快速排序

2010-03-10 15:36:52

交换机

2010-03-10 13:29:01

以太网交换机

2009-06-11 13:39:18

命名惯例Java

2010-03-19 11:12:23

Java Socket

2010-08-25 13:48:51

CSSlist-style-

2009-08-11 14:55:44

C#枚举
点赞
收藏

51CTO技术栈公众号