按字母顺序对 ArrayList<> 进行排序,根据标志位的标记分为两部分。

huangapple go评论89阅读模式
英文:

Sort ArrayList<> Alphabetically in two parts depends if flag is marked

问题

@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);
}
英文:

I have List of objects which has several several members the most important are fullName and isSubscribed. I want sort it alphabetically in the following order:

  1. First show alphabetically ordered objects that isSubscribed flag true.
  2. After that show also alphabetically sorted objects that isSubscribed flag is false.

example of expected Contacts order:

  1. Barry (isSubscribed:true)

  2. Ericsson (isSubscribed:true)

  3. Andy (isSubscribed:false)

  4. Cook (isSubscribed:false)

I call sort method of my list which has overridden method compareTo(). my code is alphabetically ordering properly for flag marked false, however flag marked true is just putting on top of that list without alphabetically order.
example of Contacts order that I have:

  1. Ericsson (isSubscribed:true)

  2. Barry (isSubscribed:true)

  3. Andy (isSubscribed:false)

  4. Cook (isSubscribed:false)

My overridden code of my Comparable Object (I cant use java 8 due to some circumstances(min sdk level), so please dont offer java8 solution):

   @Override
public int compareTo(PersonalContact contact) {
String fullName = getFullName() != null ? getFullName() : &quot;&quot;;
String contactFullName = contact.getFullName() != null ? contact.getFullName() : &quot;&quot;;
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() : &quot;&quot;;
String contactId = contact.getAndroidId() != null ? contact.getAndroidId() : &quot;&quot;;
if (id.equals(contactId)) {
List&lt;ContactNumberOrAddress&gt; noas1 = getNumbersOrAddresses();
List&lt;ContactNumberOrAddress&gt; noas2 = contact.getNumbersOrAddresses();
if (noas1.size() == noas2.size() &amp;&amp; noas1.size() &gt; 0) {
if (!noas1.containsAll(noas2) || !noas2.containsAll(noas1)) {
for (int i = 0; i &lt; 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() : &quot;&quot;;
String contactOrg =
contact.getOrganization() != null ? contact.getOrganization() : &quot;&quot;;
return org.compareTo(contactOrg);
}
return id.compareTo(contactId);
}
return fullName.compareTo(contactFullName);
}

答案1

得分: 1

我建议只是为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:这种方法类似于其他答案,但更通用。

英文:

I would suggest just to use custom comparator for Collections.sort

Like

    class PersonalContactComparator implements Comparator&lt;PersonalContact&gt;
{
@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;
}
}

And use it as Collections.sort(list, new PersonalContactComparator());

PS: This approach is similar to another asnwers, but more general one

答案2

得分: 1

如果我理解正确,您可以使用以下方法:

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

或者如果我没记错的话(我现在不确定)。布尔值是可比较的:

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

这些“comparing”方法为任何左侧(LHS)和右侧(RHS)提供了一个比较键,然后可以对它们进行比较。

不幸的是,目前没有“comparingBoolean”方法。

英文:

If I understood you may use the following:

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

Or if I am not mistaken (I am not certain atm). Boolean being a Comparable:

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

The ~comparing~ methods give a comparison key for any of LHS and RHS, which then can be compared.

Unfortunately there is no comparingBoolean.

答案3

得分: 0

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

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());
    }
}
英文:

There probably a better way, but here my solution

 static void sort(final List&lt;PersonalContact&gt; contacts)
{
final List&lt;PersonalContact&gt; subscribed = new ArrayList&lt;&gt;();
final List&lt;PersonalContact&gt; unsubscribed = new ArrayList&lt;&gt;();
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&lt;PersonalContact&gt;
{
@Override
public int compare(final PersonalContact o1, final PersonalContact o2)
{
return o1.getFullName().compareTo(o2.getFullName());
}
}

huangapple
  • 本文由 发表于 2020年10月27日 21:06:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64555121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定