Posted on 2009-06-09 23:43
Gavin.lee 阅读(1486)
评论(0) 编辑 收藏 所属分类:
java SE & EE
还不错的通用排序
package com.Gavin.tools.sortlist;
import java.sql.Date;
class Person {
public Person() {
}
private String name;
private int age;
private long salary;
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.Gavin.tools.sortlist;
import java.lang.reflect.Method;
import java.util.Comparator;
class CommonComparator implements Comparator {
/** *//***************************************************************************
* @param 比较对象的属性用String[]的形式传过来
* 比较的对象一定要符合javaBean,即要有Set,Get方法
*/
String[] fields_user = null;
public String[] getFields_user() {
return fields_user;
}
public void setFields_user(String[] fields_user) {
this.fields_user = fields_user;
}
/** *//**
* 定义排序规则 如果按照不止一个属性进行排序 这按照属性的顺序进行排序,类是sql order by 即只要比较出同位置的属性就停止
*/
public int compare(Object obj1, Object obj2) {
// 没有属性,则不排序
if (fields_user == null || fields_user.length <= 0) {
return 2;// 不比较
}
for (int i = 0; i < fields_user.length; i++) {
if (compareField(obj1, obj2, fields_user[i])) {
// 如果obj1的属性值大于obj2的属性值,则返回正数
return 1;
} else {
// 小于返回负数
return -1;
}
}
// 相等返回0
return 0;
}
/** *//**
* @param fieldName
* 根据属性名排序
*/
private static boolean compareField(Object o1, Object o2, String fieldName) {
try {
String value1 = getFieldValueByName(fieldName, o1).toString();
String value2 = getFieldValueByName(fieldName, o2).toString();
if (value1.compareTo(value2) > 0) {
return true;
}
} catch (Exception e) {
System.out.println("-----------------------------------------------------------------------------");
System.out.println("---------对象的该属性不存在或者不允许在此安全级别上反射该属性,详情请查阅JAVA DOC--------");
System.out.println("-----------------------------------------------------------------------------");
e.printStackTrace();
}
return false;
}
/** *//**
* @param fieldName
* 属性名 obj 对象 反射获得该属性的值
*/
private static Object getFieldValueByName(String fieldName, Object obj) {
try {
String Letter = fieldName.substring(0, 1).toUpperCase();
String methodStr = "get" + Letter + fieldName.substring(1);
Method method = obj.getClass().getMethod(methodStr, new Class[] {});
Object value = method.invoke(obj, new Object[] {});
return value;
} catch (Exception e) {
System.out.println("------------------------------------------------------");
System.out.println("---------该" + fieldName + "属性不存在----------------------");
System.out.println("------------------------------------------------------");
return null;
}
}
}
package com.Gavin.tools.sortlist;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Collections;
public class ObjectSort {
public List<Person> createlist() {
List<Person> demolist = new ArrayList<Person>();
Person p1 = new Person();
p1.setName("张三");
p1.setAge(20);
p1.setSalary(2000);
Person p2 = new Person();
p2.setName("李四");
p2.setAge(22);
p2.setSalary(1800);
Person p3 = new Person();
p3.setName("王五");
p3.setAge(25);
p3.setSalary(1900);
Person p4 = new Person();
p4.setName("赵六");
p4.setAge(25);
p4.setSalary(3000);
Person p5 = new Person();
p5.setName("刘七");
p5.setAge(30);
p5.setSalary(2000);
Person p6 = new Person();
p6.setName("马八");
p6.setAge(26);
p6.setSalary(1900);
demolist.add(p1);
demolist.add(p2);
demolist.add(p3);
demolist.add(p4);
demolist.add(p5);
demolist.add(p6);
return demolist;
}
public static void main(String[] args) {
/** *//***********************************************************************
* 根据对象的某个或者某些属性排序
* 使用时只要将对象属性列表和需要排序的List传入即可
**********************************************************************/
List<Person> demolist = new ObjectSort().createlist();
// *********开始比较********//
CommonComparator comparator = new CommonComparator();
System.out.println("------1.按照月薪和年龄从低到高比较---------");
comparator.setFields_user(new String[] {"salary", "age" });
Collections.sort(demolist, comparator);
for (int i = 0; i < demolist.size(); i++) {
Person person = (Person) demolist.get(i);
System.out.println("用户名:" + person.getName() + " 年龄:" + person.getAge() + " 月薪:" + person.getSalary());
}
}
}