Sort an ArrayList by primitive boolean type

前端 未结 8 861
北恋
北恋 2020-12-09 16:08

I want to sort my ArrayList using a boolean type. Basically i want to show entries with true first. Here is my code below:

Abc.java

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 16:33

    why dont use something like this, its easier and 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

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

    output: false,false,true,true,true

提交回复
热议问题