Sort an ArrayList by primitive boolean type

前端 未结 8 881
北恋
北恋 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:39

    A simple suggestion would be to use the object Boolean instead of boolean and use 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.

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

提交回复
热议问题