返回顶部

收藏

List 排序 Java工具类

更多
/**
#############################################################################
# DESCRIBE 关于给Vector,ArrayList和LinkedList特定List集合排序的方法
# AUTHOR 
# DATE 2007-3-16
# COMPANY FLX
# PORJECT JAVA
#############################################################################
*/
import java.io.PrintStream;
import java.text.CollationKey;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortList {

 public static void main(String args[]) {
  String words[] = { "man", "Man", "Woman", "woman", "Manana", "manana", "ma?ana", "Ma?ana", "Mantra", "mantra",
    "mantel", "Mantel" };

  PrintStream out = System.out;

  List list = new ArrayList(Arrays.asList(words));
  out.println("传入的list:");
  out.println(list);
  out.println();

  Collections.sort(list);
  out.println("默认的sorting:");
  out.println(list);
  out.println();

  list = new ArrayList(Arrays.asList(words));
  Comparator comp = Collections.reverseOrder();
  Collections.sort(list, comp);
  out.println("反向sorting:");
  out.println(list);
  out.println();

  list = new ArrayList(Arrays.asList(words));
  comp = new CollatorComparator();
  Collections.sort(list, comp);
  out.println("自定义排序sorting:");
  out.println(list);
  out.println();

  list = new ArrayList(Arrays.asList(words));
  comp = new CaseInsensitiveComparator();
  Collections.sort(list, comp);
  out.println("按照语种sorting:");
  out.println(list);
  out.println();

 }

 /**
  #############################################################################
  #
  # @DESCRIBE 自定义的排序规则
  # @author Administrator
  #
  #############################################################################
  */
 public static class CaseInsensitiveComparator implements Comparator {
  public int compare(Object element1, Object element2) {
   String lower1 = element1.toString().toLowerCase();
   String lower2 = element2.toString().toLowerCase();
   return lower1.compareTo(lower2);
  }
 }
 /**
  #############################################################################
  #
  # @DESCRIBE 按照语种进行排序的规则
  # @author Administrator
  #
  #############################################################################
  */
 public static class CollatorComparator implements Comparator {
  Collator collator = Collator.getInstance();

  public int compare(Object element1, Object element2) {
   CollationKey key1 = collator.getCollationKey(element1.toString());
   CollationKey key2 = collator.getCollationKey(element2.toString());
   return key1.compareTo(key2);
  }
 }
}

标签:java

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. zhuangli 发表 2018-10-06 11:04:53 ImageJ的插件开发
  2. 刘莉莉 发表 2018-10-06 16:37:11 Spring Cloud OpenFeign集成Protocol Buffer
  3. Harries 发表 2018-10-06 11:05:53 什么是Hibernate延时加载?
  4. darida 发表 2018-10-06 11:36:37 Java Bean与Map之间相互转化的实现
  5. 刘莉莉 发表 2018-10-06 14:03:53 负载,性能测试工具-Gatling
  6. xiaoli.wang 发表 2018-10-06 15:03:36 java和go中的密码学-系列教程目录
  7. hellas 发表 2018-10-06 15:04:36 Spring Boot整合Dubbo
  8. hanze 发表 2018-10-03 17:49:58 springBoot整合dubbo
  9. songhua.gao 发表 2018-10-03 19:19:30 Netty LineBasedFrameDecoder 剖析(Netty Bug)
  10. puefu.he 发表 2018-10-03 21:20:56 Spring Boot的@Configuration和自动配置有什么区别?
  11. wenming.gapo 发表 2018-10-03 22:21:11 java开发比特币类库bitcoinj入门指南
  12. hellas 发表 2018-10-04 00:52:35 Java 11已经发布

发表评论