I have a user of a class that contains a logical field, I want to sort the list of users, I want users who have a logical field to be true to be at the top of the list, and I want to sort them by their names. Here is my class:
public class User{ int id; String name; boolean myBooleanField; public User(int id, String name, boolean myBooleanField){ this.id = id; this.name = name; this.myBooleanField = myBooleanField; } @Override public boolean equals(Object obj) { return this.id == ((User) obj).id; } }
Here is an example to clear what I want: let's say I have this collection of users:
ArrayList<User> users = new ArrayList<User>(); users.add(new User(1,"user1",false)); users.add(new User(2,"user2",true)); users.add(new User(3,"user3",true)); users.add(new User(4,"user4",false)); Collections.sort(users, new Comparator<User>() { @Override public int compare(User u1, User u2) {
I want to sort users to get this result:
user2 user3 user1 user4
source share