-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)


});

`

Shahzeb
  • 85
  • 9
  • what is `By a custom Key` ? – Ravi Jan 25 '18 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? – bkis Jan 25 '18 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 – Shahzeb Jan 25 '18 at 16:47

1 Answers1

1

Your object need to implement the Comparable interface.

See javadoc

melwil
  • 2,547
  • 1
  • 19
  • 34
Antoine Dubuis
  • 4,974
  • 1
  • 15
  • 29