0

I have a Java class named Friend

public class Friend {   
    public String friendsUserName;
    public boolean isFavorite;
    public boolean isFriend;
}

I get Json file from the server containing JsonArray of this class Friend. I use Gson class to parse and map the Json to the ArrayList<Friend>.I have to sort the array in following manner

  1. Top level elements are Friend who are isFavorite and isFriend
  2. Then the ArrayList contains remaining Friend who are only isFriend
  3. Finally the ArrayList contains Friend where isFriend is False.

So for that I can use the method described on this ComparatorChain

Or I can use the following way to properly sort the ArrayList<Friend>

 public ArrayList<Friend> friendsList;
 public void sortArrayList() {
    ArrayList<Friend> favoriteList = new ArrayList<Friend>();
    ArrayList<Friend> friendOnlyList = new ArrayList<Friend>();
    ArrayList<Friend> nonFriendList = new ArrayList<Friend>();

    for (int length = friendsList.size(), i = length - 1; i >= 0; i--) {
        Friend friend = friendsList.get(i);
        if (friend.isFriend) {
            if (friend.isFavorite) {
                favoriteList.add(friend);
            } else {
                friendOnlyList.add(friend);
            }
        } else {
            nonFriendList.add(friend);
        }
        friendsList.remove(i);
    }
    addAllAndClear(favoriteList);
    favoriteList = null;

    addAllAndClear(friendOnlyList);
    friendOnlyList = null;
    addAllAndClear(nonFriendList);
    nonFriendList = null;

}

public void addAllAndClear(ArrayList<Friend> updatedList) {
    Collections.sort(updatedList, nameComparator);
    friendsList.addAll(updatedList);
    updatedList.clear();
    updatedList = null;

}

    Comparator<Friend> nameComparator = new Comparator<FriendListResponse.Friend>() {
    @Override
    public int compare(Friend lhs, Friend rhs) {
        return lhs.friendsUserName.compareTo(rhs.friendsUserName);
    };
};

Efficiency wise which should I follow the ComparatorChain or my own method. I am developing this for Android platform,so Memory Management and Efficiency is of topmost priority. P.S. I am no good at using tools to compare Efficiency.

1
  • Other posters have already given potential solutions, but a good resource to refer to when optimizing for performance is the Performance Tips article offered in the Android Documentation Commented Jun 17, 2014 at 13:34

2 Answers 2

2

Try following comparator:

Comparator<Friend> favoriteComparator = new Comparator<FriendListResponse.Friend>() {
    @Override
    public int compare(Friend lhs, Friend rhs) {
        int i=0,j=0;
        if(lhs.isFavorite()&&lhs.isFriend())
           i++;
        if(lhs.isFriend())
           i++;
        if(rhs.isFavorite()&&rhs.isFriend())
           j++;
        if(rhs.isFriend())
           j++; 
        return i-j;
    };
};

While sorting:

Collections.sort(friendsList,favoriteComparator);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer,it works like a charm.And one thing, since I want the List to contain Friend who is both isFavorite and isFriend at top of the List,followed by Friend who is isFriend only and rest non friends.So for that I need to return j-i instead of return i-j
Is it possible to find the from which index there is changing of Friend nature. For e.g. A,B are Friendwho are fav and frnd, C,D,E are Friend who are frnd only and F,G are non frnd. So, the changing indices are 0 2 5. I can do it easily from sorted list,but is it possible to do from above comparator.Meaning, sorting and keeping changed indices can be done at same time or not. Thanks
I don't think this comparator can be used to achieve this. You should simply apply a for loom on the sorted list and find the indexes.
Thanks. I will surely implement using for loop to the sorted list
2

In your class Friend implement Comparable interface and then use Collections.sort()

Or as @vipul mittal suggest

Comparator may look like

Comparator<Friend> favoriteComparator = new Comparator<FriendListResponse.Friend>() {
    @Override
    public int compare(Friend lhs, Friend rhs) {
        int i=0,j=0;
        if(lhs.isFavorite() && lhs.isFriend())
           i++;
        if(lhs.isFriend())
           i++;
        if(rhs.isFavorite() && rhs.isFriend())
           j++;
        if(rhs.isFriend())
           j++; 

        if (i==j) {
            return lhs.friendsUserName.compareTo(rhs.friendsUserName);
        }
        else {
            return i-j;
        }
    };
};

2 Comments

this . The ComparatorChain is doing exactly this , only it take care of the chaining logic for you. All you need to do is place the logic handling isFavourite first and the one handling the isFriend last
@Unlink: Thanks for the answer and thanks for implementing the compareTo(friendUserName)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.