【发布时间】:2018-11-08 20:53:29
【问题描述】:
我正在尝试使用比较器对象对从数据库中获取的对象列表进行排序。它应该比较 last names 并且如果 last names 相同,它应该比较 first names 并确定它们的顺序,所以如果我有类似的列表那:
[Jon Doe][Zed Adams][John Adams]
应该是这样排序的:
[John Adams][Zed Adams][Jon Doe]
现在让我们看看我的代码:
比较器类:
public class ComparatorContactByName implements Comparator<Contact> {
@Override
public int compare(Contact c1, Contact c2) {
// if lastNames of compared objects are not the same, compare them
if(!c1.getLastName().toLowerCase().equals(c1.getLastName().toLowerCase())){
return c1.getLastName().compareTo(c2.getLastName());
// if lastNames are the same, compare by firstName
}else if(c1.getLastName().toLowerCase().equals(c1.getLastName().toLowerCase())){
return c1.getFirstName().toLowerCase().compareTo(c2.getFirstName().toLowerCase());
// other case like firstName and lastName are the same, compare by id
}else{
return c1.getContactId() - c2.getContactId();
}
}
}
控制器方法:
public void getAllContactsSortedByName(){
List<Contact> allContacts = ContactRepository.listAllContacts();
Comparator comparatorContactByName = new ComparatorContactByName();
Collections.sort(allContacts, comparatorContactByName);
if (allContacts == null) {
System.out.println("No contact found. ");
} else {
for (Contact contact : allContacts) {
System.out.println(contact.toString());
}
}
}
调用此方法后,我得到如下输出:
Contact{contactId= 133, firstName= John, lastName= Adams, email= ja@email.com, groups= [gym]}
Contact{contactId= 126, firstName= Jon, lastName= Doe, email= jd@email.com, groups= [work, gym]}
Contact{contactId= 130, firstName= Zed, lastName= Adams, email= za@email.com, groups= [work]}
“Zed”应该是第二名,但他是最后一名。任何想法如何解决这个逻辑?
【问题讨论】:
-
在 Java 8+ 中:
Comparator.comparing(Contact::getLastName).thenComparing(Concat::getFirstName).thenComparingInt(Contact::getConcatId). -
在“
c1.getLastName().toLowerCase().equals(c1.getLastName().toLowerCase())”中:您使用了两次c1。无论如何都要使用c1.getLastName().equalsIgnoreCase(c2.getLastName())。 -
在 compare() 中,你的 if 语句比较 c1 和 c1,而不是比较 c1 和 c2。
-
还要注意
else if的条件是if条件的补集,所以你永远不会到达最后的else。
标签: java sorting collections java-8 comparator