В Kotlin:
val list = arrayListOf(false, true, false, true, false)
list.sortBy { it == false }
вывод:
[true, true, false, false, false]
А вот пример, если мы хотим отсортировать объект:
// Your model class
class SomeObject(var value: String, var state: Boolean)
...
val list = arrayListOf<SomeObject>(
SomeObject("some value", false),
SomeObject("some value2", true),
SomeObject("some value3", false),
SomeObject("some value4", false),
SomeObject("some value5", true))
list.sortBy { it.state == false }
В Java:
// Model ->
public class SomeObject {
private String name;
private Boolean state;
public SomeObject(String name, Boolean state) {
this.name = name;
this.state = state;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getState() {
return state;
}
public void setState(Boolean state) {
this.state = state;
}
}
...
ArrayList<SomeObject> list = new ArrayList<>();
list.add(new SomeObject("some value", false));
list.add(new SomeObject("some value2", true));
list.add(new SomeObject("some value3", false));
list.add(new SomeObject("some value4", false));
list.add(new SomeObject("some value5", true));
list.sort((o1, o2) -> Boolean.compare(!o1.getState(), !o2.getState()));
list.forEach((it) -> System.out.println(it.getState()));
вывод:
[true, true, false, false, false]