java中的ArrayList需要通过collections类的sort方法来进行排序

如果想自定义排序方式则需要有类来实现Comparator接口并重写compare方法

调用sort方法时将ArrayList对象与实现Commparator接口的类的对象作为参数

示例:

// 外部类的方式

import java.util.ArrayList; import java.util.Collections;

import java.util.Comparator; import java.util.List;

public class Test {  public static void main(String[] args) {

Student zlj = new Student("丁晓宇", 21);

Student dxy = new Student("赵四", 22);

Student cjc = new Student("张三", 11);

Student lgc = new Student("刘武", 19);

List<Student> studentList = new ArrayList<Student>();

studentList.add(zlj);

studentList.add(dxy);

studentList.add(cjc);

studentList.add(lgc);

Collections.sort(studentList, new SortByAge());

for (Student student : studentList) {

System.out.println(student.getName() + " / " + student.getAge());

}

System.out.println("  =  ");

Collections.sort(studentList, new SortByName());

for (Student student : studentList) {

System.out.println(student.getName() + " / " + student.getAge());

}  } }

class SortByAge implements Comparator {

public int compare(Object o1, Object o2) {

Student s1 = (Student) o1;   Student s2 = (Student) o2;

if (s1.getAge() > s2.getAge())    return 1;   return 0;  } }

class SortByName implements Comparator {

public int compare(Object o1, Object o2) {

Student s1 = (Student) o1;

Student s2 = (Student) o2;

return s1.getName().compareTo(s2.getName());  } }

//匿名内部类的方式

//按照受益人级别进行排序

Collections.sort(beneficiarys,new Comparator(){

@Override

public int compare(Object o1, Object o2) {

Beneficiary s1 = (Beneficiary) o1;

Beneficiary s2 = (Beneficiary) o2;

if (Integer.parseInt(s1.getBeneficiaryOrder()) > Integer.parseInt(s2.getBeneficiaryOrder()))

return 1;

return 0;

}    });

定制对ArrayList的sort方法的自定义排序的更多相关文章

  1. java中sort方法的自定义比较器写法(转载)

    java中sort方法的自定义比较器写法 摘要 在做一些算法题时常常会需要对数组.自定义对象.集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collecti ...

  2. 用Java集合中的Collections.sort方法对list排序的两种方法

    用Collections.sort方法对list排序有两种方法第一种是list中的对象实现Comparable接口,如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  3. sort方法和自定义比较器的写法

    摘要 在做一些算法题时常常会需要对数组.自定义对象.集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collections.sort()方法.对自定义对象排序 ...

  4. java中Collections.sort()方法实现集合排序

    1.Integer/String泛型的List进行排序 List <Integer> integerlist = new ArrayList<Integer>();   //定 ...

  5. 【C++】标准库sort函数的自定义排序

    自定义排序需要单独写一个compare函数 例1 LeetCode 056. Merge Intervals Given a collection of intervals, merge all ov ...

  6. (JavaScript基础向)sort()方法里的排序函数的理解

    比较常见的解释可以看这里:js的sort()方法,这篇博客写得挺好的,一般的应用的理解已经足够了. 但是如果要活用sort()方法里面的参数——也就是排序函数的话,可能就比较难理解了. 然后我就总结出 ...

  7. Collections.sort方法对list排序的两种方式

    Collections.sort( )分为两部分,一部分为排序规则,一部分为排序算法 . 规则用来判断对象,算法则考虑如何进行排序 对于自定义对象,sort()不知道规则,所以无法比较,这种情况下一定 ...

  8. Java基础集锦——利用Collections.sort方法对list排序

    要想对List进行排序,可以让实体对象实现Comparable接口,重写compareTo方法即可实现按某一属性排序,但是这种写法很单一,只能按照固定的一个属性排序,没变法变化.通过下面这种方法,可以 ...

  9. ArrayList 排序Sort()方法扩展

    1.sort() sort可以直接对默认继承 IComparable接口的类进行排序,如:int.string.... ArrayList arrayList = new ArrayList(); , ...

随机推荐

  1. 综合支撑【恶灵附身 Psycho Break】的世界观的概念艺术

    综合支撑[恶灵附身  Psycho Break]的世界观的概念艺术 三上真司监督提出的,是对着重表现讲述内心恐怖的哪个世界观的创作和统一做了很大贡献的概念艺术. 这里以他经手的艺术为例,来看看稀少的恐 ...

  2. C++类型转化分析(1)

    仔细想想地位卑贱的类型转换功能(cast),其在程序设计中的地位就象goto语句一样令人鄙视.但是它还不是无法令人忍受,因为当在某些紧要的关头,类型转换还是必需的,这时它是一个必需品. 不过C风格的类 ...

  3. php 分词 —— PHPAnalysis无组件分词系统

    分词,顾名思义就是把词语分开,从哪里分开?当然是一大堆词语里了,一大堆词语是什么?是废话或者名言.这在数据库搜索时非常有用. 官方网站 http://www.phpbone.com/phpanalys ...

  4. 删除下标为n的数组值

    Array.prototype.del=function(n) { //n表示第几项,从0开始算起.//prototype为对象原型,注意这里为对象增加自定义方法的方法. if(n<0) //如 ...

  5. 【总结】使用jdbc+servlet开发一个bug管理系统的经验总结

    开发背景: 公司目前使用Teambition里面的task作为bug管理系统,既没有bug的当前状态,也不能写上bug的详细复现步骤,被assign了任务(该修复bug或者验证bug是否被修复)也没有 ...

  6. Delphi 缩放图像代码 - 支持PNG透明通道

    要求Delphi2007或者更高版本, 系统要求至少XP-SP2以上 实际上是利用了Windows的windowscodecs.dll这个文件的功能 在VCL里已经封装为TWICImage类 proc ...

  7. insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a=10001

    insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a ...

  8. java对象中继承和变量初始化顺序浅析

    先上例子代码 public class F { int age = 5; public F() { print(); } public void print() { System.out.printl ...

  9. HttpPost发送Json

    1.public static JSONObject post(String url,JSONObject json){ 2. HttpClient client = new DefaultHttpC ...

  10. Microsoft Dynamics AX 2009 White Paper: Close Non-Financial Transfers

    http://www.microsoft.com/en-us/download/confirmation.aspx?id=12174