通用对象按属性排序类
CompareUtil.java
public class CompareUtil<T> implements Comparator<T> {
private ArrayList<SortItem> sortItemList = new ArrayList<SortItem>();
private class SortItem {
public String fieldName;
public String sortType;
public boolean isAsc;
}
@Override
public int compare(T arg0, T arg1) {
// TODO Auto-generated method stub
int result = 0;
for (int i = 0; i < sortItemList.size(); i++) { // 多个属性比较
SortItem tempItem = sortItemList.get(i);
if (tempItem.sortType.contains("String")) { // 字符串比较
String s1 = getFieldValueByName(arg0, tempItem.fieldName).toString();
String s2 = getFieldValueByName(arg1, tempItem.fieldName).toString();
if (tempItem.isAsc) { // 升序
result = ascStr(s1, s2);
if (result != 0) { // 已经比较出大小
return result;
}
} else {
result = descStr(s1, s2);
if (result != 0) {
return result;
}
}
} else { // 整数比较
int s1 = 0;
try {
s1 = Integer.parseInt(String.valueOf(getFieldValueByName(arg0, tempItem.fieldName)));
} catch (Exception e) {
// TODO: handle exception
}
int s2 = 0;
try {
s2 = Integer.parseInt(String.valueOf(getFieldValueByName(arg1, tempItem.fieldName)));
} catch (Exception e) {
// TODO: handle exception
}
if (tempItem.isAsc) { // 升序
result = ascInt(s1, s2);
if (result != 0) {
return result;
}
} else {
result = descInt(s1, s2);
if (result != 0) {
return result;
}
}
}
}
return 0;
}
/**
* 添加排序条件
* @param fieldName 排序属性名
* @param sortType 按什么类型排序
* @param isAsc 是否是升序
* @return
*/
public CompareUtil<T> addSort(String fieldName, String sortType, boolean isAsc) {
SortItem item = new SortItem();
item.fieldName = fieldName;
item.sortType = sortType;
item.isAsc = isAsc;
sortItemList.add(item);
return this;
}
/**
* 获取指定名称的域值
* @param obj 获取数据的对象
* @param fieldName 获取数据的域名
* @return
*/
private Object getFieldValueByName(Object obj, String fieldName) {
try {
String Letter = fieldName.substring(0, 1).toUpperCase();
String methodStr = "get" + Letter + fieldName.substring(1);
Method method = obj.getClass().getMethod(methodStr, new Class[] {});
return method.invoke(obj, new Object[] {});
} catch (Exception e) {
return null;
}
}
/**
* 升序
* @param s1
* @param s2
* @return
*/
private int ascInt(int s1, int s2) {
if (s1 > s2) {
return -1;
} else if (s1 < s2) {
return 1;
} else {
return 0;
}
}
/**
* 降序
* @param s1
* @param s2
* @return
*/
private int descInt(int s1, int s2) {
if (s1 > s2) {
return 1;
} else if (s1 < s2) {
return -1;
} else {
return 0;
}
}
/**
* 升序
* @param s1
* @param s2
* @return
*/
private int ascStr(String s1, String s2) {
if (s1.compareToIgnoreCase(s1) > 0) {
return -1;
} else if (s1.compareToIgnoreCase(s2) < 0) {
return 1;
} else {
return 0;
}
}
/**
* 降序
* @param s1
* @param s2
* @return
*/
private int descStr(String s1, String s2) {
if (s1.compareToIgnoreCase(s2) > 0) {
return 1;
} else if (s1.compareToIgnoreCase(s2) < 0) {
return -1;
} else {
return 0;
}
}
}
使用方法:
public class MainActivity extends Activity {
private ArrayList<Person> arrayDemo = new ArrayList<Person>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initValues();
initListeners();
}
private void initViews() {
}
private void initValues() {
initData();
print();
CompareUtil<Person> comparator = new CompareUtil<Person>();
comparator.addSort("name", String.class.getName(), false);
comparator.addSort("age", Integer.class.getName(), false);
Collections.sort(arrayDemo, comparator);
print();
}
private void initListeners() {
}
private void print() {
for (int i = 0; i < arrayDemo.size(); i++) {
Log.i("test", arrayDemo.get(i).toString());
}
Log.i("test", "11111111111111111111111111111111111");
}
private void initData() {
Person p1 = new Person();
p1.setName("zhang san");
p1.setAge("22");
p1.setSalary(160);
Person p2 = new Person();
p2.setName("zhang san");
p2.setAge("28");
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("32");
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);
}
}
Person.java
public class Person {
private String name;
private String age;
private long salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}