简体   繁体   English

按原始布尔类型对 ArrayList 进行排序

[英]Sort an ArrayList by primitive boolean type

I want to sort my ArrayList using a boolean type.我想使用布尔类型对我的ArrayList进行排序。 Basically i want to show entries with true first.基本上我想首先显示true条目。 Here is my code below:这是我的代码如下:

Abc.java驱动程序

public class Abc {
 int id;
 bool isClickable;

 Abc(int i, boolean isCl){
    this.id = i;
    this.isClickable = iCl;
 }
 }

Main.java主程序

List<Abc> abc = new ArrayList<Abc>();

//add entries here

//now sort them
Collections.sort(abc, new Comparator<Abc>(){
        @Override
        public int compare(Abc abc1, Abc abc2){

            boolean b1 = abc1.isClickable;
            boolean b2 = abc2.isClickable;

            if (b1 == !b2){
                return 1;
            }
            if (!b1 == b2){
                return -1;
            }
            return 0;
        }
    });

Order before sorting: true true true false false false false true false false排序前排序:true true true false false false false true false false

Order after sorting: false false true true true true false false false false排序后的顺序: false false true true true false false false false

Another way to go is:另一种方法是:

Collections.sort(abc, new Comparator<Abc>() {
        @Override
        public int compare(Abc abc1, Abc abc2) {
            return Boolean.compare(abc2.isClickable,abc1.isClickable);
        }
    });

In this case one of the easiest solutions is to convert booleans to integers, where false is 0 and true is 1 .在这种情况下,最简单的解决方案之一是将布尔值转换为整数,其中false0true1 Then return the difference of the second one and the first one.然后返回第二个和第一个的差异。

So:所以:

        int b1 = abc1.isClickable ? 1 : 0;
        int b2 = abc2.isClickable ? 1 : 0;

        return b2 - b1

should do it.应该这样做。

why dont use something like this, its easier and java 8为什么不使用这样的东西,它更容易和 java 8

listOfABCElements = {true, false, false, true, true};
listOfABCElements.stream().sorted(Comparator.comparing(Abc::isClickable,Comparator.reverseOrder()).collect(Collectors.toList());

output: true,true,true,false,false输出:真、真、真、假、假

reverseOrder is for order first true and after false, in other case falses goes first reverseOrder 用于顺序先真后假,在其他情况下假先行

listOfABCElements.stream().sorted(Comparator.comparing(Abc::isClickable).collect(Collectors.toList());

output: false,false,true,true,true输出:假,假,真,真,真

I want the items with true value to appear first.我希望具有true价值的项目首先出现。 My solution would be:我的解决方案是:

Collections.sort(m_mall, new Comparator<Mall>(){

        @Override
        public int compare(Mall mall1, Mall mall2){

            boolean b1 = mall1.isClickable;
            boolean b2 = mall2.isClickable;

            return (b1 != b2) ? (b1) ? -1 : 1 : 0;
        }
    });

Using Kotlin you can do smth like this:使用 Kotlin,你可以这样做:

listOfABCElements.sortBy { it.isClickable }

output: false,false,true,true,true输出:假,假,真,真,真

in reverse order:以相反的顺序:

listOfABCElements.sortByDescending { it.isClickable }

output: true,true,true,false,false输出:真、真、真、假、假

A simple suggestion would be to use the object Boolean instead of boolean and use Collections.sort .一个简单的建议是使用对象Boolean而不是 boolean 并使用Collections.sort

However, you must know that the false will be before the true because true are represented as 1 and false as 0 .但是,您必须知道false将在true之前,因为 true 表示为1 , false 表示为0 But then, you could just change your algorithm and access in reverse order.但是,您可以更改算法并以相反的顺序访问。

Edit : As soulscheck stated, you could use Collections.reverseOrder to revert the ordering imposed by the Comparator.编辑:正如soulscheck 所说,您可以使用Collections.reverseOrder来恢复比较器强加的排序。

Java 8:爪哇 8:

 Collections.sort(abc, (abc1, abc2) ->
                  Boolean.compare(abc2.isClickable(), abc1.isClickable()));

也可以这样。

myList.sort((a, b) -> Boolean.compare(a.isSn_Principal(), b.isSn_Principal()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM