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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28002342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:26:06  来源:igfitidea点击:

Sort an ArrayList by primitive boolean type

javasortingarraylistjava-collections-api

提问by stud91

I want to sort my ArrayListusing a boolean type. Basically i want to show entries with truefirst. Here is my code below:

我想ArrayList使用布尔类型对我的进行排序。基本上我想首先显示条目true。这是我的代码如下:

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

回答by Jean-Fran?ois Savard

A simple suggestion would be to use the object Booleaninstead of boolean and use Collections.sort.

一个简单的建议是使用 objectBoolean而不是 boolean 并使用Collections.sort.

However, you must know that the falsewill be before the truebecause true are represented as 1and false as 0. But then, you could just change your algorithm and access in reverse order.

但是,您必须知道falsewill 在 之前,true因为 true 表示为 ,1而 false表示为0。但是,您可以更改算法并以相反的顺序访问。

Edit: As soulscheck stated, you could use Collections.reverseOrderto revert the ordering imposed by the Comparator.

编辑:正如soulscheck 所说,您可以使用Collections.reverseOrder来恢复比较器强加的排序。

回答by soulcheck

In this case one of the easiest solutions is to convert booleans to integers, where falseis 0and trueis 1. Then return the difference of the second one and the first one.

在这种情况下,最简单的解决方案之一是将布尔值转换为整数,其中falseis0trueis 1。然后返回第二个和第一个的差异。

So:

所以:

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

        return b2 - b1

should do it.

应该这样做。

回答by stud91

I want the items with truevalue to appear first. My solution would be:

我希望具有true价值的项目首先出现。我的解决方案是:

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

回答by Danilo

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

回答by vphilipnyc

Java 8:

爪哇 8:

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

回答by davidddp

It is also possible that way.

也可以这样。

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

回答by leonardo rey

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

输出:假,假,真,真,真

回答by Alex Koba

Using Kotlin you can do smth like this:

使用 Kotlin,你可以这样做:

listOfABCElements.sortBy { it.isClickable }

output: true,true,true,false,false

输出:真、真、真、假、假

in reverse order:

以相反的顺序:

listOfABCElements.sortByDescending { it.isClickable }

output: false,false,true,true,true

输出:假,假,真,真,真