排序数组列表<>按字母顺序分为两部分取决于是否标记了标志



我有一个对象列表,它有几个成员,最重要的是fullName和isSubscribed。我想按以下顺序按字母顺序排序:

  1. 首先显示按字母顺序排列的对象,其下标标志为true
  2. 之后还显示按字母顺序排序的对象,isSubscribed标志为false

预期联系人订单示例:

  1. Barry(isSubscribed:true(

  2. 爱立信(isSubscribed:true(

  3. Andy(isSubscribed:false(

  4. Cook(isSubscribed:false(

我调用列表的排序方法,该方法已重写方法compareTo((。对于标记为false的标志,我的代码是按字母顺序正确排序的,然而标记为true的标志只是放在列表的顶部,而没有按字母顺序排序。我的联系人订单示例:

  1. 爱立信(isSubscribed:true(

  2. Barry(isSubscribed:true(

  3. Andy(isSubscribed:false(

  4. Cook(isSubscribed:false(

我的可比较对象的重写代码(由于某些情况(最低sdk级别(,我无法使用java8,所以请不要提供java8解决方案(:

@Override
public int compareTo(PersonalContact contact) {
String fullName = getFullName() != null ? getFullName() : "";
String contactFullName = contact.getFullName() != null ? contact.getFullName() : "";
int c;
PersonalContact c1 = this;
PersonalContact c2 = contact;
String fullNameContact1 = c1.getFullName();
String fullNameContact2 = c2.getFullName();
Boolean subscribedForPresenceContact1 = c1.isSubscribeForPresenceEnabled();
Boolean subscribedForPresenceContact2 = c2.isSubscribeForPresenceEnabled();
c = subscribedForPresenceContact1.compareTo(subscribedForPresenceContact2);
if (subscribedForPresenceContact1) {
return  -1;
} else if (subscribedForPresenceContact2) {
return 1;
} else  if (fullName.equals(contactFullName)) {
String id = getAndroidId() != null ? getAndroidId() : "";
String contactId = contact.getAndroidId() != null ? contact.getAndroidId() : "";
if (id.equals(contactId)) {
List<ContactNumberOrAddress> noas1 = getNumbersOrAddresses();
List<ContactNumberOrAddress> noas2 = contact.getNumbersOrAddresses();
if (noas1.size() == noas2.size() && noas1.size() > 0) {
if (!noas1.containsAll(noas2) || !noas2.containsAll(noas1)) {
for (int i = 0; i < noas1.size(); i++) {
int compare = noas1.get(i).compareTo(noas2.get(i));
if (compare != 0) return compare;
}
}
} else {
return Integer.compare(noas1.size(), noas2.size());
}
String org = getOrganization() != null ? getOrganization() : "";
String contactOrg =
contact.getOrganization() != null ? contact.getOrganization() : "";
return org.compareTo(contactOrg);
}
return id.compareTo(contactId);
}
return fullName.compareTo(contactFullName);
}

我建议只使用Collections.sort的自定义比较器

class PersonalContactComparator implements Comparator<PersonalContact>
{
@Override
public int compare(final PersonalContact o1, final PersonalContact o2)
{
if (o1.isSubscribeForPresenceEnabled() == o2.isSubscribeForPresenceEnabled()) {
return o1.getFullName().compareTo(o2.getFullName());
}
if (o1.isSubscribeForPresenceEnabled()) {
return 1;
}
return -1;
}
}

并将其用作Collections.sort(list, new PersonalContactComparator());

PS:这种方法类似于另一种asnwers,但更通用的是一种

如果我理解,您可以使用以下内容:

list.sort(Comparator.comparingInt(pc -> pc.isSubscribeForPresenceEnabled() ? 0 : 1)
.thenComparing(PersonalContact::getFullName));

或者如果我没有错的话(我不确定atm(。布尔可比性:

list.sort(Comparator.comparing(PersonalContac::isSubscribeForPresenceEnabled)
.reversed()
.thenComparing(PersonalContact::getFullName));

~比较~方法为LHS和RHS中的任何一个提供了一个比较键,然后可以进行比较。

遗憾的是,没有comparingBoolean。

可能有更好的方法,但这里是我的解决方案

static void sort(final List<PersonalContact> contacts)
{
final List<PersonalContact> subscribed = new ArrayList<>();
final List<PersonalContact> unsubscribed = new ArrayList<>();
for(final PersonalContact contact : contacts)
{
if(contact.isSubscribeForPresenceEnabled())
subscribed.add(contact);
else
unsubscribed.add(contact);
}
final PersonalContactComparator personalContactComparator = new PersonalContactComparator();
subscribed.sort(personalContactComparator);
unsubscribed.sort(personalContactComparator);
contacts.clear();
contacts.addAll(subscribed);
contacts.addAll(unsubscribed);
}
private static class PersonalContactComparator implements Comparator<PersonalContact>
{
@Override
public int compare(final PersonalContact o1, final PersonalContact o2)
{
return o1.getFullName().compareTo(o2.getFullName());
}
}

最新更新