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
Sort an ArrayList by primitive boolean type
提问by stud91
I want to sort my ArrayList
using a boolean type. Basically i want to show entries with true
first. 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 Boolean
instead of boolean and use Collections.sort
.
一个简单的建议是使用 objectBoolean
而不是 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
. But then, you could just change your algorithm and access in reverse order.
但是,您必须知道false
will 在 之前,true
因为 true 表示为 ,1
而 false表示为0
。但是,您可以更改算法并以相反的顺序访问。
Edit: As soulscheck stated, you could use Collections.reverseOrder
to 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 false
is 0
and true
is 1
. Then return the difference of the second one and the first one.
在这种情况下,最简单的解决方案之一是将布尔值转换为整数,其中false
is0
和true
is 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 true
value 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
输出:假,假,真,真,真