-1

I have an Arraylist and I want to sort ArrayList by two properties 1.By Name(Alphabetical Order) 2.By a custom Key

More Explanation -:

ArrayList<User> list = new ArrayList();

Collections.sort(list, new Comparator<User>() {
        @Override
        public int compare(User user, User t1) {
        1.By User.getName() (Alphabetically)
        2.By User.getRelationshipState() (Custom property)


});

`

3
  • what is By a custom Key ? Commented Jan 25, 2018 at 16:45
  • What do you mean by "custom Key"? Do you want to change the key to sort for everytime you sort? Or is this just some field of the User class? Commented Jan 25, 2018 at 16:46
  • Custom key like user.getRelationshipState which is public final static int REQNOTSENT =0; public final static int REQUESTSENT =1; public final static int FRIENDS=2; public final static int REQUESTRCVED =3; I want to sort like Friends REQUESTSENT REQUESTRCVED REQNOTSENT Commented Jan 25, 2018 at 16:47

1 Answer 1

1

Your object need to implement the Comparable interface.

See javadoc

Sign up to request clarification or add additional context in comments.

3 Comments

Kindly Can you explain this litter bit pls?
This doesn't even sounds like a potential comment.
Sorry, but this won't take him further than what he already tried with the Comparator.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.