sort()排序 collections.sort();
1.main方法:
public class Test {
public static void main(String[] args) {
/**
*
* sort()方法详解
* 1.Collections.sort(List<T> list)
* 根据元素的自然顺序 对指定列表按升序进行排序。
* 2.Collections.sort(List<T> list, Comparator<? super T> c)
* 根据指定比较器产生的顺序对指定列表进行排序。
*
*/
List<Integer> list = new ArrayList<Integer>();
list.add();
list.add();
list.add();
//自然顺序
Collections.sort(list);
for(Integer i:list){
System.out.println(i);
}
System.out.println("===============================================");
Point point2 = new Point(,,);
Point point1 = new Point(,,);
Point point3 = new Point(,,);
List<Point> points = new ArrayList<Point>();
points.add(point2);
points.add(point1);
points.add(point3);
System.out.println("===============================================");
//根据point中的升序输出
Collections.sort(points, new SortByXdesc());
for(Point point:points){
System.out.println("x:"+point.getX()+" y:"+point.getY()+" z:"+point.getZ());
}
System.out.println("===============================================");
//根据point中的x降序输出
Collections.sort(points, new SortByXasc());
for(Point point:points){
System.out.println("x:"+point.getX()+" y:"+point.getY()+" z:"+point.getZ());
}
}
}
2.降序输出类SortByXdesc:
public class SortByXdesc implements Comparator<Object> {
//根据point中的x降序输出
@Override
public int compare(Object o1, Object o2) {
Point point1 =(Point)o1;
Point point2 =(Point)o2;
if(point1.getX()>point2.getX()){
return 1;
}else{
return 0;
}
}
}
3.升序输出类SortByXasc:
public class SortByXasc implements Comparator<Object> {
//根据point中的x升序输出
@Override
public int compare(Object o1, Object o2) {
Point point1 =(Point)o1;
Point point2 =(Point)o2;
if(point1.getX()>point2.getX()){
return 0;
}else{
return 1;
}
}
}
转:http://www.blogjava.net/secret/archive/2011/12/03/352813.html
sort()排序 collections.sort();的更多相关文章
- 对List数组进行排序 Collections.sort(param1,param2)
@SuppressWarnings("unchecked") List<PageData> group_items_list = (List<PageData&g ...
- 48- java Arrays.sort和collections.sort()再次总结
今天又碰到一个新BUG,记下来. 一直报空指针异常,我就很奇怪了,怎么就空指针了呢,我输出时,也能输出东西呀. 原来Arrays.sort() 和 Collections.sort() 都是对整个数组 ...
- Arrays.sort和Collections.sort实现原理解析
Arrays.sort和Collections.sort实现原理解析 1.使用 排序 2.原理 事实上Collections.sort方法底层就是调用的array.sort方法,而且不论是Collec ...
- Arrays.sort 与 Collections.sort
代码如下: package com.wangzhu.arrays; import java.util.Arrays; import java.util.Collections; public clas ...
- 160415、sql语句sort排序,sort为空的在后面
按sort排序,sort为空的在后面 select * from 表名 order by (case when sort is null or sort='' then 1 else 0 end),s ...
- 160317(二)、按sort排序,sort为空的在后面
按sort排序,sort为空的在后面 select * from 表名 order by (case when sort is null or sort='' then 1 else 0 end),s ...
- java List 排序 Collections.sort() 对 List 排序
class User { String name; String age; public User(String name,String age){ this.name=name; this.a ...
- java List 排序 Collections.sort()
用Collections.sort方法对list排序有两种方法 第一种是list中的对象实现Comparable接口,如下: /** * 根据order对User排序 */ public class ...
- List排序Collections.sort 重写compare
static List<Integer> intList = Arrays.asList(2,5,7, 3, 1); public static void main(String[] ar ...
随机推荐
- simhash--文本排重
转载自 https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/06.12.md http: ...
- 【BZOJ 2820】 YY的GCD (莫比乌斯+分块)
YY的GCD Description 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少 ...
- 配置.NET程序使用代理进行HTTP请求
方式一:代码方式 var defaultProxy = new WebProxy(); defaultProxy.Address = new Uri("http://proxy:8080&q ...
- 【简译】jQuery对象的奥秘:基础介绍
本文翻译自此文章 你有没有遇到过类似$(".cta").click(function(){})这样的JavaScript代码并且在想“$('#x')是什么”?如果这些对你想天书一样 ...
- Android Mediaplayer各种属性和方法简单介绍
主要涉及类:MediaPlayer (1) 当一个MediaPlayer对象被创建或者调用reset()方法之后,它处于空闲状态,调用release()方法后处于结束状态 1,一个MediaPlaye ...
- MySQL show status详解
http://www.sandzhang.com/blog/2010/04/07/mysql-show-status-explained-detail/ 要查看MySQL运行状态,要优化MySQL运行 ...
- lsof 拥有更多的功能
lsof 拥有更多的功能# lsof -i 看系统中有哪些开放的端口,哪些进程.用户在使用它们,比 netstat -lptu 的输出详细. # lsof -i 4 查看IPv4类型的进程COMMA ...
- ☀【移动】Google Maps JavaScript API v3
Google Maps JavaScript API v3https://developers.google.com/maps/documentation/javascript/tutorial?hl ...
- Number Sequence ----HDOJ 1711
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Oracle statspack 安装及使用
转载:http://blog.csdn.net/joinplay/article/details/23358133 oracle statspack 工具从oracle 8.1.6开始被引用,从ora ...