定制对ArrayList的sort方法的自定义排序
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方法的自定义排序的更多相关文章
- java中sort方法的自定义比较器写法(转载)
java中sort方法的自定义比较器写法 摘要 在做一些算法题时常常会需要对数组.自定义对象.集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collecti ...
- 用Java集合中的Collections.sort方法对list排序的两种方法
用Collections.sort方法对list排序有两种方法第一种是list中的对象实现Comparable接口,如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- sort方法和自定义比较器的写法
摘要 在做一些算法题时常常会需要对数组.自定义对象.集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collections.sort()方法.对自定义对象排序 ...
- java中Collections.sort()方法实现集合排序
1.Integer/String泛型的List进行排序 List <Integer> integerlist = new ArrayList<Integer>(); //定 ...
- 【C++】标准库sort函数的自定义排序
自定义排序需要单独写一个compare函数 例1 LeetCode 056. Merge Intervals Given a collection of intervals, merge all ov ...
- (JavaScript基础向)sort()方法里的排序函数的理解
比较常见的解释可以看这里:js的sort()方法,这篇博客写得挺好的,一般的应用的理解已经足够了. 但是如果要活用sort()方法里面的参数——也就是排序函数的话,可能就比较难理解了. 然后我就总结出 ...
- Collections.sort方法对list排序的两种方式
Collections.sort( )分为两部分,一部分为排序规则,一部分为排序算法 . 规则用来判断对象,算法则考虑如何进行排序 对于自定义对象,sort()不知道规则,所以无法比较,这种情况下一定 ...
- Java基础集锦——利用Collections.sort方法对list排序
要想对List进行排序,可以让实体对象实现Comparable接口,重写compareTo方法即可实现按某一属性排序,但是这种写法很单一,只能按照固定的一个属性排序,没变法变化.通过下面这种方法,可以 ...
- ArrayList 排序Sort()方法扩展
1.sort() sort可以直接对默认继承 IComparable接口的类进行排序,如:int.string.... ArrayList arrayList = new ArrayList(); , ...
随机推荐
- django 模板中定义临时列表
<ul class="num_t clr"> {% for obj in ""|ljust:"10" %} <li> ...
- php防攻击方法
php防攻击方法 更多答案 请参考 @如何有效防止XSS攻击/AJAX跨域攻击 我说下防止非法用户的一些常用手段吧 1 前端的js验证: 我认为js验证只是一种用户体验的提升,对普通用户群体的简单 ...
- intervention/image intervention/imagecache
http://image.intervention.io/ 安装两个包 composer require intervention/image composer require interventio ...
- Python For Data Analysis -- NumPy
NumPy作为python科学计算的基础,为何python适合进行数学计算,除了简单易懂,容易学习 Python可以简单的调用大量的用c和fortran编写的legacy的库 Python科学计算的这 ...
- selenium By 元素定位详解
转自:http://blog.sina.com.cn/s/blog_6966650401012a6u.html WebDriver拾级而上·之三 定位页面元素 selenium-webdriver提供 ...
- Java反射机制深入研究
ava 反射是Java语言的一个很重要的特征,它使得Java具体了“动态性”. 在Java运行时环境中,对于任意一个类,能否知道这个类有哪些属性和方法?对于任意一个对象,能否调用它的任意一个方法? ...
- ubuntu如何开启root,如何启用Ubuntu中root帐号
jingyan.baidu.com/article/495ba84116104238b20ede62.html ubuntu如何开启root,如何启用Ubuntu中root帐号 | 浏览:8344 | ...
- php--城市分类
效果图:
- QcheckBox
#include "dialog.h" #include "ui_dialog.h" #include <QtCore> #include < ...
- 采用asyncore进行实时同步
最近在维护项目的时候,发现某个实时数据同步功能非常容易失败,故静下心来彻底弄清楚该设计的实现原理,以及其中用到的python异步sockethandler : asyncore. 实时数据同步功能的设 ...