下文可以直接拿来运行使用,也可以直接下载附件包,编译直接运行就可以看到效果。
//ObjectSort
package sort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ObjectSort {
/**
* @param args
*/
public static void main(String[] args) {
/**
*
* purpose: 根据对象的某个或者某些属性排序
* Java里面包含8个基本数据类型,分别是:boolean、byte、char、short、int、float、douboe、long
* 只比较非boolean和byte类型的属性,和大多数运用环境适应,如果有特殊要求,请自己修改。
* 使用时只要将对象属性列表和需要排序的List传入即可
* **/
List arrayDemo = new ArrayList();
Person p1 = new Person();
p1.setName("zhang san");
p1.setAge(22);
p1.setSalary(160);
Person p2 = new Person();
p2.setName("zhang san");
p2.setAge(22);
p2.setSalary(150);
Person p3 = new Person();
p3.setName("zhang san");
p3.setAge(25);
p3.setSalary(1900);
Person p4 = new Person();
p4.setName("zhao liu");
p4.setAge(25);
p4.setSalary(3000);
Person p5 = new Person();
p5.setName("liu qi");
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.sort by name, age and salary");
comparator.setFields_user(new String[]{"name","age","salary"});
Collections.sort(arrayDemo, comparator);
for(int i=0;i<arrayDemo.size();i++)
{
Person person = (Person)arrayDemo.get(i);
System.out.println("name:"+person.getName()+" age:"+person.getAge()+" salary:"+person.getSalary());
}
System.out.println("1.sort by name, age");
comparator.setFields_user(new String[]{"name","age"});
Collections.sort(arrayDemo, comparator);
for(int i=0;i<arrayDemo.size();i++)
{
Person person = (Person)arrayDemo.get(i);
System.out.println("name:"+person.getName()+" age:"+person.getAge()+" salary:"+person.getSalary());
}
}
}
//CommonComparator
package sort;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.CollationKey;
import java.text.Collator;
import java.util.Comparator;
public 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++)
{
//return compareField(obj1,obj2,fields_user[i]);
if(compareField(obj1,obj2,fields_user[i])>0)
{//如果obj1的属性值大于obj2的属性值,则返回正数
return 1;
}else if(compareField(obj1,obj2,fields_user[i])<0){
return -1;
}
}
return 0;
}
/**
* @param fieldName
* 根据属性名排序
* */
private static int compareField(Object o1,Object o2,String fieldName)
{
try
{
Object value1 = getFieldValueByName(fieldName,o1);
Object value2 = getFieldValueByName(fieldName,o2);
//--字符串比较
if(value1 instanceof String)
{
String v1 = getFieldValueByName(fieldName,o1).toString();
String v2 = getFieldValueByName(fieldName,o2).toString();
Collator myCollator = Collator.getInstance();
CollationKey[] keys = new CollationKey[5];
keys[0] = myCollator.getCollationKey(v1);
keys[1] = myCollator.getCollationKey(v2);
return (keys[0].compareTo(keys[1]));
}
//--非比较属性不比较
else if("java.lang.Boolean".equals(value1.getClass().getName()) || "java.lang.Byte".equals(value1.getClass().getName()))
{
return 0;
}else
{
BigDecimal b1 = new BigDecimal(value1.toString());
BigDecimal b2 = new BigDecimal(value2.toString());
return b1.compareTo(b2);
}
} catch (Exception e)
{
System.out.println("-----------------------------------------------------------------------------");
System.out.println("---------对象的该属性不存在或者不允许在此安全级别上反射该属性,详情请查阅JAVA DOC--------");
System.out.println("-----------------------------------------------------------------------------");
e.printStackTrace();
}
//小于
return -1;
}
/**
* @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;
}
}
}
//Person
package sort;
public 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;
}
}