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();的更多相关文章

  1. 对List数组进行排序 Collections.sort(param1,param2)

    @SuppressWarnings("unchecked") List<PageData> group_items_list = (List<PageData&g ...

  2. 48- java Arrays.sort和collections.sort()再次总结

    今天又碰到一个新BUG,记下来. 一直报空指针异常,我就很奇怪了,怎么就空指针了呢,我输出时,也能输出东西呀. 原来Arrays.sort() 和 Collections.sort() 都是对整个数组 ...

  3. Arrays.sort和Collections.sort实现原理解析

    Arrays.sort和Collections.sort实现原理解析 1.使用 排序 2.原理 事实上Collections.sort方法底层就是调用的array.sort方法,而且不论是Collec ...

  4. Arrays.sort 与 Collections.sort

    代码如下: package com.wangzhu.arrays; import java.util.Arrays; import java.util.Collections; public clas ...

  5. 160415、sql语句sort排序,sort为空的在后面

    按sort排序,sort为空的在后面 select * from 表名 order by (case when sort is null or sort='' then 1 else 0 end),s ...

  6. 160317(二)、按sort排序,sort为空的在后面

    按sort排序,sort为空的在后面 select * from 表名 order by (case when sort is null or sort='' then 1 else 0 end),s ...

  7. java List 排序 Collections.sort() 对 List 排序

    class User { String name; String age;  public User(String name,String age){  this.name=name;  this.a ...

  8. java List 排序 Collections.sort()

    用Collections.sort方法对list排序有两种方法  第一种是list中的对象实现Comparable接口,如下: /** * 根据order对User排序 */ public class ...

  9. List排序Collections.sort 重写compare

    static List<Integer> intList = Arrays.asList(2,5,7, 3, 1); public static void main(String[] ar ...

随机推荐

  1. BZOJ 1642: [Usaco2007 Nov]Milking Time 挤奶时间

    Description 贝茜是一只非常努力工作的奶牛,她总是专注于提高自己的产量.为了产更多的奶,她预计好了接下来的N (1 ≤ N ≤ 1,000,000)个小时,标记为0..N-1. Farmer ...

  2. 【技术贴】解决Mysql启动服务报错1067 进程意外终止

    无法启动MYSQL服务错误1067 进程意外终止. 我使用2013-10-25_appserv-win32-2.6.0.exe安装的MySql,结果服务起不来. 于是细心机智的我发现,在F:\stud ...

  3. Spring 自动装配

    1.自动装配有 bytype 和byName两种模式. 2.可以使用autowire属性指定自动装配的方式,byName根据bean的名称和当前bean的setter风格属性进行自动装配:byType ...

  4. 移动应用产品开发-android开发(一)

    最近公司希望增添移动开发业务,进行移动互联网开发的调研及产品需求调研. 我主要负责技术解决方案的研究,从android开发开始学习.同时跟经理一起与其他部门同事沟通了解移动开发方面的需求. 在了解an ...

  5. LREM key count value

    LREM key count value Available since 1.0.0. Time complexity: O(N) where N is the length of the list. ...

  6. Php使用sqlite

    php sqlite文档:http://php.net/manual/en/book.sqlite.php sql:http://www.php100.com/html/webkaifa/PHP/PH ...

  7. hadoop2.2编程:mapreduce编程之二次排序

    mr自带的例子中的源码SecondarySort,我重新写了一下,基本没变. 这个例子中定义的map和reduce如下,关键是它对输入输出类型的定义:(java泛型编程) public static ...

  8. C#反射之基础应用

    今天把反射的东西整理了一下 , 提供了最全面的东西 , 当然也是基础的东西 ,在学好了这一切的基础上 , 大家可以学习反射的具体插件等应用 首先我们建立一个类库 , 将它生成为 reflectPrj  ...

  9. C# Read/Write another Process' Memory ZZ

    Today's tutorial is about...processes' memory! In this article I'll show you how to read/write a pro ...

  10. zoj 3620 Escape Time II

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4744 Escape Time II Time Limit: 2 Seconds ...