我做了一个功能通过在模型类特定变量排序对象的列表。它可能不会直接匹配你需要的,但也许你可以接受这个想法。我使用反射来排序任何数据类型的类模型。
public Vector sort(Vector list, String kelas, String s, String asc) throws NoSuchMethodException {
try {
// Creates an object of type Class which contains the information of
// the class String
Object[] obj = list.toArray();
Object[] args = {};
Class cl = Class.forName(kelas);
Method toSort = cl.getMethod(s, null);
if (asc.equalsIgnoreCase("desc")) {
if (toSort.getReturnType().toString().equalsIgnoreCase("int") || toSort.getReturnType().toString().equalsIgnoreCase("double")) {
for (int i = 0; i < obj.length; i++) {
for (int j = i; j < obj.length; j++) {
try {
if (Double.parseDouble(toSort.invoke(obj[i], args).toString()) < Double.parseDouble(toSort.invoke(obj[j], args).toString())) {
Object temp = obj[i];
obj[i] = obj[j];
obj[j] = temp;
}
} catch (IllegalAccessException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} else {
for (int i = 0; i < obj.length; i++) {
for (int j = i; j < obj.length; j++) {
try {
if (toSort.invoke(obj[i], args).toString().compareToIgnoreCase(toSort.invoke(obj[j], args).toString()) < 0) {
Object temp = obj[i];
obj[i] = obj[j];
obj[j] = temp;
}
} catch (IllegalAccessException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
} else {
if (toSort.getReturnType().toString().equalsIgnoreCase("int") || toSort.getReturnType().toString().equalsIgnoreCase("double")) {
for (int i = 0; i < obj.length; i++) {
for (int j = i; j < obj.length; j++) {
try {
if (Double.parseDouble(toSort.invoke(obj[i], args).toString()) > Double.parseDouble(toSort.invoke(obj[j], args).toString())) {
Object temp = obj[i];
obj[i] = obj[j];
obj[j] = temp;
}
} catch (IllegalAccessException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} else {
for (int i = 0; i < obj.length; i++) {
for (int j = i; j < obj.length; j++) {
try {
if (toSort.invoke(obj[i], args).toString().compareToIgnoreCase(toSort.invoke(obj[j], args).toString()) > 0) {
Object temp = obj[i];
obj[i] = obj[j];
obj[j] = temp;
}
} catch (IllegalAccessException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
list = new Vector();
for (int i = 0; i < obj.length; i++) {
list.add(obj[i]);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return list;
}
你可以调用该函数像这样简单:
Vector sortedList=sort(UnsortedList, "bean.Items", "getItemName", "asc");
该行基于项目名称升
这可以被改写将整理我的项目清单(产品模型类)为'返回this.lastName.compareTo(p.lastName);' – jlordo 2013-03-26 00:23:20
假设lastName的是一个字符串,你可以这样做:返回this.lastName.toLower()的compareTo(p.lastName.toLower());并保存自己的所有其他代码。不过,字符串中的比较是区分大小写的,所以要小心。 – Kylar 2013-03-26 00:24:44