不说别的,直接发代码,哎
java web 开发很有用
package test;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Collections;
public class ObjectSort {
/**
* @param args
*/
public static void main(String[] args) {
/**
*
* author: youjianbo
* date:2008-6-27
* purpose: 根据对象的某个或者某些属性排序
* 使用时只要将对象属性列表和需要排序的List传入即可
* **/
List arrayDemo = new ArrayList();
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);
arrayDemo.add(p1);
arrayDemo.add(p2);
arrayDemo.add(p3);
arrayDemo.add(p4);
arrayDemo.add(p5);
//*********开始比较********//
CommonComparator comparator = new CommonComparator();
// System.out.println("------1.按照年龄从小到大比较---------");
// comparator.setFields_user(new String[]{"age"});
// Collections.sort(arrayDemo, comparator);
// for(int i=0;i<arrayDemo.size();i++)
// {
// Person person = (Person)arrayDemo.get(i);
// System.out.println("用户名:"+person.getName()+" 年龄:"+person.getAge()+" 月薪:"+person.getSalary());
// }
// System.out.println("------1.按照月薪从低到高比较---------");
// comparator.setFields_user(new String[]{"salary"});
// Collections.sort(arrayDemo, comparator);
// for(int i=0;i<arrayDemo.size();i++)
// {
// Person person = (Person)arrayDemo.get(i);
// System.out.println("用户名:"+person.getName()+" 年龄:"+person.getAge()+" 月薪:"+person.getSalary());
// }
// System.out.println("------1.按照年龄和月薪从低到高比较---------");
// comparator.setFields_user(new String[]{"age","salary"});
// Collections.sort(arrayDemo, comparator);
// for(int i=0;i<arrayDemo.size();i++)
// {
// Person person = (Person)arrayDemo.get(i);
// System.out.println("用户名:"+person.getName()+" 年龄:"+person.getAge()+" 月薪:"+person.getSalary());
// }
System.out.println("------1.按照月薪和年龄从低到高比较---------");
comparator.setFields_user(new String[]{"salary","age"});
Collections.sort(arrayDemo, comparator);
for(int i=0;i<arrayDemo.size();i++)
{
Person person = (Person)arrayDemo.get(i);
System.out.println("用户名:"+person.getName()+" 年龄:"+person.getAge()+" 月薪:"+person.getSalary());
}
}
}
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;
}
}
}
class Person
{
public Person(){}
private String name;
private int age;
private long salary;
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;
}
}
java web 开发很有用
package test;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Collections;
public class ObjectSort {
/**
* @param args
*/
public static void main(String[] args) {
/**
*
* author: youjianbo
* date:2008-6-27
* purpose: 根据对象的某个或者某些属性排序
* 使用时只要将对象属性列表和需要排序的List传入即可
* **/
List arrayDemo = new ArrayList();
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);
arrayDemo.add(p1);
arrayDemo.add(p2);
arrayDemo.add(p3);
arrayDemo.add(p4);
arrayDemo.add(p5);
//*********开始比较********//
CommonComparator comparator = new CommonComparator();
// System.out.println("------1.按照年龄从小到大比较---------");
// comparator.setFields_user(new String[]{"age"});
// Collections.sort(arrayDemo, comparator);
// for(int i=0;i<arrayDemo.size();i++)
// {
// Person person = (Person)arrayDemo.get(i);
// System.out.println("用户名:"+person.getName()+" 年龄:"+person.getAge()+" 月薪:"+person.getSalary());
// }
// System.out.println("------1.按照月薪从低到高比较---------");
// comparator.setFields_user(new String[]{"salary"});
// Collections.sort(arrayDemo, comparator);
// for(int i=0;i<arrayDemo.size();i++)
// {
// Person person = (Person)arrayDemo.get(i);
// System.out.println("用户名:"+person.getName()+" 年龄:"+person.getAge()+" 月薪:"+person.getSalary());
// }
// System.out.println("------1.按照年龄和月薪从低到高比较---------");
// comparator.setFields_user(new String[]{"age","salary"});
// Collections.sort(arrayDemo, comparator);
// for(int i=0;i<arrayDemo.size();i++)
// {
// Person person = (Person)arrayDemo.get(i);
// System.out.println("用户名:"+person.getName()+" 年龄:"+person.getAge()+" 月薪:"+person.getSalary());
// }
System.out.println("------1.按照月薪和年龄从低到高比较---------");
comparator.setFields_user(new String[]{"salary","age"});
Collections.sort(arrayDemo, comparator);
for(int i=0;i<arrayDemo.size();i++)
{
Person person = (Person)arrayDemo.get(i);
System.out.println("用户名:"+person.getName()+" 年龄:"+person.getAge()+" 月薪:"+person.getSalary());
}
}
}
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;
}
}
}
class Person
{
public Person(){}
private String name;
private int age;
private long salary;
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;
}
}