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 1902: Zju2116 Christopher lucas定理 && 数位DP

    1902: Zju2116 Christopher Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 172  Solved: 67[Submit][Stat ...

  2. eclipse打开文件目录

    在MyEclipse开发中常用到其中一个"Open In Explorer"的小插件,可以直接进入Windows资源管理器中打开选中文件所在的目录,在使用eclipse开发时也很需 ...

  3. C++ Socket UDP "Hello World!"

    这是C++ SOCKET网络程序中的C/S结构之UDP "Hello World !",共两个控制台工程: //////////////////////////////////// ...

  4. Golang全接触

    满打满算, 从好友推荐Golang至发文时, 使用Golang已经有1年多了. 这种时间对于C/C++ Java这些老者来说, 简直是菜鸟级别的经验 但作为新生代语言的特点就是实战. Golang这一 ...

  5. 【Xamarin挖墙脚系列:Xamarin开发环境配置需求】

    原文:[Xamarin挖墙脚系列:Xamarin开发环境配置需求] 前言 因为操作的全是大块头,加大你们的内存,CPU网上飙.... 卤煮的机器配置  最近的版本部署包,百度云离线下载:版本:Xama ...

  6. (转载)AS3中的mouseEnabled与mouseChildren

    (转载)http://www.cnblogs.com/gongchen/archive/2013/05/09/3069055.html mouseEnabled与mouseChildren都是用来确定 ...

  7. 逐行返回http响应的内容

    前言 问题:1.什么是特殊字符? 2.为什么要处理特殊字符? 答:特殊字符指相对于传统或常用的符号外,使用频率较少字符且难以直接输入的符号,比如数学符号:单位符号:制表符等 有些符号在URL中是不能直 ...

  8. Android学习笔记(十四)方便实用的首选项-PreferenceActivity

    突然发现已经好多天没更新博客了,最近公司项目正在进行一个大跨度的重构,又碰上有新需求,一连好多天都是很晚才到家.其实这篇博文在草稿箱里面也存了很久了,本来想着不发了,不过感觉PreferenceAct ...

  9. HTML页面规范

    <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title> ...

  10. 常用的Git命令

    我的常用的Git命令 Git仓库配置常用 1. clone 克隆一份远程的Git版本库 git clone git://github.com/someone/some_project.git some ...