英文:
Java Comparator Interface NullsLast with anonymous inner class
问题
if (!sortProperty.equals("") && sortProperty != null) {
Collections.sort(list, new Comparator<T>() {
public int compare(T e1, T e2) {
Method sortGetter = null;
try {
for (PropertyDescriptor pd : Introspector.getBeanInfo(entity).getPropertyDescriptors()) {
if (pd.getName().equals(sortProperty) && pd.getReadMethod() != null) {
sortGetter = pd.getReadMethod();
}
}
} catch (IntrospectionException e) {
e.printStackTrace();
}
try {
if (sortGetter.getReturnType().equals(String.class)) {
return sortGetter.invoke(e1).toString().compareTo(sortGetter.invoke(e2).toString());
} else if (sortGetter.getReturnType().equals(Integer.TYPE)) {
return (Integer) sortGetter.invoke(e1) - (Integer) sortGetter.invoke(e2);
} else if (sortGetter.getReturnType().equals(Date.class)) {
return ((Date) sortGetter.invoke(e1)).compareTo((Date) sortGetter.invoke(e2));
} else if (sortGetter.getReturnType().equals(Timestamp.class)) {
return ((Timestamp) sortGetter.invoke(e1)).compareTo((Timestamp) sortGetter.invoke(e2));
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
return 0;
}
});
}
英文:
i've created a method that returns the raw html for a table in java based on the given entity (i use JPA). Now i'd like to be able to sort the table with the comparator interace. So i created an anonymous inner class of the comparator interface, to sort after attributes of the entities. This works fine, but my problem is now that i'm not able to sort NullsFirst or Nullslast. Everything i've tried doesn't works. Does anyone got an idea how to solve this?
This is the code:
if (!sortProperty.equals("") && sortProperty != null) {
Collections.sort(list, new Comparator<T>() {
public int compare(T e1, T e2) {
Method sortGetter = null;
try {
for (PropertyDescriptor pd : Introspector.getBeanInfo(entity).getPropertyDescriptors()) {
if (pd.getName().equals(sortProperty) && pd.getReadMethod() != null) {
sortGetter = pd.getReadMethod();
}
}
} catch (IntrospectionException e) {
e.printStackTrace();
}
try {
if (sortGetter.getReturnType().equals(String.class)) {
return sortGetter.invoke(e1).toString().compareTo(sortGetter.invoke(e2).toString());
} else if (sortGetter.getReturnType().equals(Integer.TYPE)) {
return (Integer) sortGetter.invoke(e1) - (Integer) sortGetter.invoke(e2);
} else if (sortGetter.getReturnType().equals(Date.class)) {
return ((Date) sortGetter.invoke(e1)).compareTo((Date) sortGetter.invoke(e2));
} else if (sortGetter.getReturnType().equals(Timestamp.class)) {
return ((Timestamp) sortGetter.invoke(e1)).compareTo((Timestamp) sortGetter.invoke(e2));
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
return 0;
}
});
}
答案1
得分: 0
将您的sortGetter.invoke(e1)
和sortGetter.invoke(e2)
提取到变量中(为了清晰起见,省略了try/catch):
Object o1 = sortGetter.invoke(e1);
Object o2 = sortGetter.invoke(e2);
检查它们中是否有任何一个为空,如果是,返回:
if (o1 == null || o2 == null) {
// 这将导致空值排在最后。对于将空值排在最前面,请使用!=。
return Boolean.compare(o1 == null, o2 == null);
}
现在您可以继续使用if (sortGetter.getReturnType().equals(String.class))
类型的逻辑(显然您不需要再次调用sortGetter.invoke(e1)
和sortGetter.invoke(e2)
)。
英文:
Pull out your sortGetter.invoke(e1)
and sortGetter.invoke(e2)
to variables (try/catch omitted for clarity):
Object o1 = sortGetter.invoke(e1);
Object o2 = sortGetter.invoke(e2);
Check if either is null, and if so, return:
if (o1 == null || o2 == null) {
// This results in nulls last. Use != for nulls first.
return Boolean.compare(o1 == null, o2 == null);
}
Now you can proceed with your if (sortGetter.getReturnType().equals(String.class))
-type logic (you obviously don't need to invoke sortGetter.invoke(e1)
and sortGetter.invoke(e2)
again).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论