我做了一個功能通過在模型類特定變量排序對象的列表。它可能不會直接匹配你需要的,但也許你可以接受這個想法。我使用反射來排序任何數據類型的類模型。
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