如何在java中使用布尔值对ArrayLists进行排序? [英] How to sort ArrayLists using booleans in java?

查看:33
本文介绍了如何在java中使用布尔值对ArrayLists进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义对象的 ArrayList.它们包含一个我想要排序的复选框对象.我正在使用此比较器功能对其进行排序:

I have an ArrayList with custom objects. They contain a checkbox object that I want to sort on. I am using this comparator function to sort it:

我正在使用 XOR 运算符来检查它们是否彼此相等,然后取反.

I am using the XOR operator to check if they are equal to each other, then negate it.

但是这不起作用,并且列表保持相同的顺序.

However this is not working, and the list is staying in the same order.

有人知道怎么回事吗?

public class CustomSelectSort implements Comparator<ObjPerson> {
    @Override
    public int compare(ObjPerson o1, ObjPerson o2) {
        return !(o1.select.isChecked() ^ o2.select.isChecked()) ? 1 : -1;
    }
}

推荐答案

你只返回 -1(小于)或 +1(大于),从不返回 0(等于).

You return only -1 (less than) or +1 (greater than), never 0 (equals to).

请参阅 java.util.Comparator 定义 :

比较它的两个参数的顺序.返回一个负整数,零或正整数,因为第一个参数小于等于到或大于第二个.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

在前面的描述中,符号 sgn(expression) 表示数学符号函数,它被定义为返回其中之一-1、0 或 1 根据表达式的值是负数、零还是正数.

In the foregoing description, the notation sgn(expression) designates the mathematical signum function, which is defined to return one of -1, 0, or 1 according to whether the value of expression is negative, zero or positive.

实现者必须确保 sgn(compare(x, y)) == -sgn(compare(y,x)) 对于所有 x 和 y.(这意味着 compare(x, y) 必须抛出一个异常当且仅当 compare(y, x) 抛出异常.)

The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

实现者还必须确保关系是可传递的:((compare(x, y)>0) && (compare(y, z)>0)) 意味着 compare(x, z)>0.

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

最后,实现者必须确保 compare(x, y)==0 意味着sgn(compare(x, z))==sgn(compare(y, z)) 对于所有 z.

Finally, the implementor must ensure that compare(x, y)==0 implies that sgn(compare(x, z))==sgn(compare(y, z)) for all z.

通常是这种情况,但并不严格要求 (compare(x,y)==0) == (x.equals(y)).一般来说,任何比较器违反此条件应明确表明这一事实.这推荐的语言是注意:这个比较器强加了以下顺序与等于不一致."

It is generally the case, but not strictly required that (compare(x, y)==0) == (x.equals(y)). Generally speaking, any comparator that violates this condition should clearly indicate this fact. The recommended language is "Note: this comparator imposes orderings that are inconsistent with equals."

Java 1.7 之前的提案:

Proposal before Java 1.7 :

public int compare(ObjPerson o1, ObjPerson o2) {
   boolean b1 = o1.select.isChecked();
   boolean b2 = o2.select.isChecked();
   if( b1 && ! b2 ) {
      return +1;
   }
   if( ! b1 && b2 ) {
      return -1;
   }
   return 0;
}

提案 自 Java 1.7 起 :

public int compare(ObjPerson o1, ObjPerson o2) {
   boolean b1 = o1.select.isChecked();
   boolean b2 = o2.select.isChecked();
   return Boolean.compare( b1, b2 );
}

这篇关于如何在java中使用布尔值对ArrayLists进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆