在List的排序中常用的是Collections.sort()方法,可以对String类型和Integer类型泛型的List集合进行排序。

首先演示sort()方法对Integer类型泛型的List排序

 /*
* 通过Collections.sort()方法,对Integer类型的泛型List进行排序
*/
public void testSort1(){
List<Integer> integerList = new ArrayList<Integer>();
//插入100以内的十个随机数
Random ran = new Random();
Integer k;
for(int i=0;i<10;i++){
do{
k = ran.nextInt(100);
}while(integerList.contains(k));
integerList.add(k);
System.out.println("成功添加整数"+k);
} System.out.println("\n-------排序前------------\n"); for (Integer integer : integerList) {
System.out.print("元素:"+integer);
}
Collections.sort(integerList);
System.out.println("\n-------排序后------------\n");
for (Integer integer : integerList) {
System.out.print("元素:"+integer);
}
}

打印输出的结果为:

成功添加整数4
成功添加整数56
成功添加整数85
成功添加整数8
成功添加整数14
成功添加整数89
成功添加整数96
成功添加整数0
成功添加整数90
成功添加整数63 -------排序前------------ 元素:4元素:56元素:85元素:8元素:14元素:89元素:96元素:0元素:90元素:63
-------排序后------------ 元素:0元素:4元素:8元素:14元素:56元素:63元素:85元素:89元素:90元素:96

对String类型泛型的List进行排序

 /*
* 对String泛型的List进行排序
*/
public void testSort2(){
List<String> stringList = new ArrayList<String>();
stringList.add("imooc");
stringList.add("lenovo");
stringList.add("google");
System.out.println("\n-------排序前------------\n");
for (String str : stringList) {
System.out.print("元素:"+str);
}
Collections.sort(stringList);
System.out.println("\n-------排序后------------\n");
for (String str : stringList) {
System.out.print("元素:"+str);
}
}

打印输出的结果为:

-------排序前------------

元素:imooc元素:lenovo元素:google
-------排序后------------ 元素:google元素:imooc元素:lenovo

使用sort()方法对String类型泛型的List进行排序时,首先是判断首字母的顺序,首字母相同时再判断其后面的字母顺序,具体的排序规则为:

1、数字0-9;

2、大写字母A-Z;

3、小写字母a-z

最新文章

  1. 移动站适配rel=alternate PC页和H5页适配标注
  2. centos7 安装jdk7
  3. VPN常见错误码(633,628,691)的意思及修复方法
  4. JSTL标签详解以及应用实例
  5. Android笔记: 查看SDK源码
  6. javascript笔记7-事件
  7. 揭秘TPM安全芯片技术及加密应用
  8. Android开发之通过Intent启动其他App的Service
  9. pyqt5猜数游戏
  10. Docker部属Nsq集群
  11. Android Widget 开发详解(二) +支持listView滑动的widget
  12. 开源视频平台:ViMP
  13. Haproxy + Keepalived +PXC 常见错误
  14. Pythonic是什么?
  15. IsWindowVisible
  16. Android app中存储文件的路径
  17. Scala:HelloWorld
  18. Elasticsearch5.x创建索引(Java)
  19. CentOS7 yum安装Java+Apache(httpd)+Tomcat并开启自启动
  20. 基于GIT的管理

热门文章

  1. C# 正则匹配domain
  2. for 循环中 i++和 ++i
  3. oracle中substr与instr
  4. Eclipse闪退无法打开的解决方法
  5. 自定义select控件开发
  6. CSS-animations和transitions性能:浏览器到底做了什么?
  7. linux系统安装yum环境
  8. maven打包步骤_maven 构建项目
  9. ios 客户端定位的3种方法
  10. 01knockout应用开发之遍历简单数据$Index、$data