按两个条件比较两个对象

时间:2015-05-04 23:11:17

标签: java comparator

我有一个包含布尔字段的类用户,我想对用户列表进行排序,我希望布尔字段等于true的用户位于列表的顶部,而不是我想要按他们的排序名。 这是我的班级:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [self adjustControllerLayout];
    [self adjustPagerNavigationBarOnScroll];
}

这是一个清除我想要的例子: 假设我有这个用户集合:

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;
    }
}

我想对用户进行排序以获得此结果:

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) {
        //Here i'm lookin for what should i add to compare the two users depending to the boolean field
        return u1.name.compareTo(u2.name);
    }
});
for(User u : users){
    System.out.println(u.name);
}

5 个答案:

答案 0 :(得分:2)

沿着这些方向可能会有什么?

Collections.sort(users, new Comparator<User>() {
    public int compare(User u1, User  u2) {
        String val1 = (u1.myBooleanField ? "0" : "1") + u1.name;
        String val2 = (u2.myBooleanField ? "0" : "1") + u2.name;

        return val1.compareTo(val2);
    }
});             

答案 1 :(得分:2)

if (u1.myBooleanField) {
    if (u2.myBooleanField)
        return u1.name.compareTo(u2.name);
    return -1;
} else if (u2.myBooleanField) {
    return 1;
} else {
    return u1.name.compareTo(u2.name);
}

答案 2 :(得分:2)

您可以先使用Boolean.compare(boolean x, boolean y)方法。由于true元素已排序到数组的开头,因此您将使用compare(u2.myBooleanField, u1.myBooleanField)

@Override
public int compare(User u1, User u2) {
    final int booleanCompare = Boolean.compare(u2.myBooleanField,
                                               u1.myBooleanField);
    if (booleanCompare != 0) {
        return booleanCompare;
    }
    return u1.name.compareTo(u2.name);
}

答案 3 :(得分:1)

if(!u1.myBooleanField && u2.myBooleanField){
return 1;
} else if (!u1.myBooleanField && u2.myBooleanField){
return -1;
} else {
//Whatever comparator you would like to sort on after sorting based on true and false
}

看看Java compareTo()方法返回的内容。在上面的例子中,我们首先基于true和false进行排序,然后,如果两个用户的myBooleanField相等,则可以根据其他属性进行排序。

答案 4 :(得分:1)

您可以使用Collectors.groupingBy分隔其他

的顶级用户
Map<Boolean, List<User>> list = users.stream()
            .collect(
                Collectors.groupingBy(
                    Info::getMyBooleanField);

选择热门用户并按名称对其进行排序

List<User> topUsers = list.get(true);
topUsers.sort((u1, u2) -> u1.getName().compareTo(u2.getName()));

选择其余用户并按名称对其进行排序:

List<User> restUsers = list.get(false);
restUsers.sort((u1, u2) -> u1.getName().compareTo(u2.getName()));

这是最终名单

topUsers.addAll(restUsers );
users=(ArrayList<User>)topUsers;