java对象排序
集合和数组的排序
示例代码如下:
StudentCompare 类实现了Comparable和Comparator两个接口
package com.compare.bean;
import java.util.Comparator;
/**
* @version 1.0
* @author HUAZAI
* @Date 2014-03-05
*/
public class StudentCompare implements Comparable<StudentCompare>,
Comparator<StudentCompare> {
private String name;
private int age;
private float score;
/**
* 实现Comparable接口的方法
*/
public int compareTo(StudentCompare student) {
if (this.score > student.score) {
return -1;
} else if (this.score < student.score) {
return 1;
} else {
if (this.age > student.age) {
return 1;
} else if (this.age < student.age) {
return -1;
} else {
return 0;
}
}
}
/**
* 实现Comparator接口的方法
*/
public int compare(StudentCompare o1, StudentCompare o2) {
if (o1.score > o2.score) {
return 1;
} else if (o1.score < o2.score) {
return -1;
} else {
if (o1.age > o2.age) {
return 1;
} else if (o1.age < o2.age) {
return -1;
} else {
return 0;
}
}
}
/**
* 带有完全参数的构造函数
*
* @param name
* @param age
* @param score
*/
public StudentCompare(String name, int age, float score) {
this.name = name;
this.age = age;
this.score = score;
}
/**
* 没有参数的构造函数
*
* @param name
* @param age
* @param score
*/
public StudentCompare() {
}
/**
* 对象格式化
*/
public String toString() {
return name + "\t" + age + "\t" + score;
}
// get/set方法
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 float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
}
测试类的代码如下:
package com.compare.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import com.compare.bean.StudentCompare;
public class CompareTest {
/**
* 生成对象数组
*
* @return StudentCompare[]
*/
public static StudentCompare[] createArray() {
StudentCompare[] stu = { new StudentCompare("Java", 22, 98),
new StudentCompare("张三", 22, 93),
new StudentCompare("李四", 20, 93),
new StudentCompare("王五", 22, 100),
new StudentCompare("马六", 21, 77),
new StudentCompare("龙七", 53, (float) 69.5) };
return stu;
}
/**
* 生成对象list
*
* @return List<StudentCompare>
*/
public static List<StudentCompare> createList() {
List<StudentCompare> list = new ArrayList<StudentCompare>();
list.add(new StudentCompare("Java", 22, 98));
list.add(new StudentCompare("张三", 22, 93));
list.add(new StudentCompare("李四", 20, 93));
list.add(new StudentCompare("王五", 22, 100));
list.add(new StudentCompare("马六", 21, 77));
list.add(new StudentCompare("龙七", 53, (float) 69.5));
return list;
}
/**
* 对象数组转化成对象list
*
* @param stu
* @return
*/
public static List<StudentCompare> arrayToList(StudentCompare[] stu) {
List<StudentCompare> list = new ArrayList<StudentCompare>();
for (int i = 0; i < stu.length; i++) {
list.add(stu[i]);
}
return list;
}
/**
* 对象list转化成对象数组
*
* @param list
* @return
*/
public static StudentCompare[] listToArray(List<StudentCompare> list) {
StudentCompare[] stu = createArray();
for (int i = 0; i < list.size(); i++) {
stu[i] = list.get(i);
}
return stu;
}
/**
* 用List排序
*
* @param list
*/
public static void sortWithList(List<StudentCompare> list) {
System.out.println("升序排列元素:");
// Collections.sort(list); //实现Comparable接口
Collections.sort(list, new StudentCompare()); // 实现Comparator接口,根据自定义比较器比较
// System.out.println("降序排列元素:");
// Collections.sort(list, Collections.reverseOrder());
// //实现Comparator接口,根据collections产生的比较器
// System.out.println("Collections.reverse 从列表中最后一个元素开始输出:");
Collections.reverse(list);
for (int i = 0; i < list.size(); i++) {
StudentCompare stucompare = list.get(i);
System.out.println(stucompare);
}
}
/**
* 用Array排序
*
* @param stu
*/
public static void sortWithArray(StudentCompare[] stu) {
// Arrays.sort(stu);////实现Comparable接口
Arrays.sort(stu, new StudentCompare());// 实现Comparator接口,根据自定义比较器比较
// System.out.println(Arrays.asList(stu));
for (int i = 0; i < 6; i++) {
System.out.println(stu[i]);
}
}
public static void main(String[] args) {
sortWithList(createList());
// sortWithArray(createArray());
}
}