Arrays 的sort 可以实现简单的排序,如果想对java bean 指定属性字段,或者是多个字段进行比较排序,我自己研究了一下comparable,可以实现指定bo属性的排序。
一)代码
package cn.com.ld.bo;
import java.util.Arrays;
public class StudentInfo implements Comparable {
private String name;
private int age;
private int grate;
private StudentInfo(String name, int age, int grate) {
this.name = name;
this.age = age;
this.grate = grate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGrate() {
return grate;
}
public void setGrate(int grate) {
this.grate = grate;
}
public int compareTo(Object o) {
int other_grate = ((StudentInfo) o).getGrate();
int other_age = ((StudentInfo) o).getAge();
int result = 0;
if (this.grate > other_grate) {
result = 1;
} else if (this.grate < other_grate) {
result = -1;
} else{
result = (this.age - other_age) > 0 ? -1 : 1 ;
}
return result;
}
public static void main(String[] args) {
StudentInfo[] siArray = new StudentInfo[] {
new StudentInfo("lida_0", 25, 94),
new StudentInfo("lida_1", 23, 95),
new StudentInfo("lida_2", 21, 99),
new StudentInfo("lida_3", 22, 99),
new StudentInfo("lida_4", 22, 100),
new StudentInfo("lida_5", 24, 98),
new StudentInfo("lida_6", 24, 96),
new StudentInfo("lida_7", 20, 97), };
Arrays.sort(siArray);
for (StudentInfo studentInfo : siArray) {
System.out.println(studentInfo.name + " " + studentInfo.age + " "
+ studentInfo.grate);
}
}
}
二)输出结果:
lida_0 25 94
lida_1 23 95
lida_6 24 96
lida_7 20 97
lida_5 24 98
lida_3 22 99
lida_2 21 99
lida_4 22 100
三)小结
public int compareTo(Object o) {
int other_grate = ((StudentInfo) o).getGrate();
int other_age = ((StudentInfo) o).getAge();
int result = 0;
if (this.grate > other_grate) {
result = 1;
} else if (this.grate < other_grate) {
result = -1;
} else{
result = (this.age - other_age) > 0 ? -1 : 1 ;
}
return result;
}
compareTo 里指定了要进行比较的属性。如果不指定比较的属性,默认针对该bean的第一个字段进行排序;升序降序,只需指定 return 的value ;
分享到:
相关推荐
1.什么是Comparable接口 此接口强行对实现它的每个类的对象进行整体排序。此排序被称为该类的自然排序 ,类的 compareTo 方法被称为它的自然比较方法 。实现此接口的对象列表(和数组)可以通过 Collections.sort ...
主要介绍了Java Comparable和Comparator对比详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Comparable的用法1---马克-to-win java视频的详细描述
主要介绍了Java Comparable 和 Comparator 的详解及区别的相关资料,Comparable 自然排序和Comparator 定制排序的实例,需要的朋友可以参考下
主要介绍了Java Comparable及Comparator接口区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
本文,先介绍Comparable 和Comparator两个接口,以及它们的差异;接着,通过示例,对它们的使用方法进行说明
java排序Comparator和Comparable
java comparable和compartor区别和联系
Java-中的-Comparator-和-Comparable.md
Java中Comparable和Comparator的区别共3页.pdf.zip
java的Comparator和Comparable
主要介绍了java 实现Comparable接口排序,升序、降序、倒叙,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
java中Comparable和Comparator的区别 简介 java.lang.Comparable和java.util.Comparator是两个容易混淆的接口,两者都带有比较的意思,那么两个接口到底有什么区别,分别在什么情况下使用呢? Comparable Comparable...
Comparable与Comparator的区别Java开发Java经验技巧共3页.pdf.zip
Comparable和Comparator接口都可用作普通意义上对象间的比大小,但两个接口在实例化方面的用法不尽相同,接下来我们就来详细对比Java中的Comparable排序接口和Comparator比较器接口
主要介绍了java 中Comparable与Comparator详解与比较的相关资料,需要的朋友可以参考下
通过简单的例子初步了解Comparable和Comparator的使用,注释很详细
Java8HashMap键与Comparable接口编程开发技术共3页.pdf.zip
主要介绍了详解Java中Comparable和Comparator接口的区别的相关资料,希望通过本文大家能彻底掌握这部分内容,需要的朋友可以参考下