加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

List 排序 Java工具类

发布时间:2020-12-15 00:14:10 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 /**############################################################################## DESCRIBE 关于给Vector,ArrayList和LinkedList特定List集合排

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

/**
#############################################################################
# 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);
  }
 }
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读