How can I sort this array of boolean
primitive, by putting false
first and true
at the end?
Suppose I have the following:
boolean[] arrayOfBoolean = // initializing the array
CodePudding user response:
There is no out-of-the-box functionality for this in the jdk. If you need to order the elements in the original array, you have to implement it yourself.
If sorting Boolean
array with the same content is acceptable, you can copy the array and sort the new array.
public class Temp {
public static void main(String[] args) {
boolean[] initial = new boolean[]{true, true, false, true};
Boolean[] result = new Boolean[initial.length];
for (int i = 0; i < initial.length; i ) {
result[i] = initial[i];
}
Arrays.sort(result);
System.out.println(Arrays.toString(result));
}
}
CodePudding user response:
You can create your own simplified version of the Counting sort algorithm by iterating over the given boolean array and accumulating the number of false
values.
Then construct a new array based on the obtained count (or reassign the values in the existing one, depending on your needs).
The time complexity is O(n) - only two iterations over the given dataset (in the best case when the given array contains only false
values - only one iteration required). Using built-in Timsort with Boolean
wrappers would be slower. Space complexity O(n) (if generating a new array doesn't required O(1)).
public static boolean[] sortBooleans(boolean[] booleans) {
int falseCount = 0;
for (boolean next: booleans) {
if (!next) falseCount ;
}
boolean[] result = new boolean[booleans.length];
for (int i = falseCount; i < result.length; i ) {
result[i] = true;
}
return result;
}
main()
public static void main(String[] args) {
boolean[] booleans = new boolean[]{true, false, true};
System.out.println(Arrays.toString(sortBooleans(booleans)));
}
Output:
[false, true, true]
CodePudding user response:
To sort this array of booleans, you can use the Arrays.sort()
method from the java.util.Arrays
class. The Arrays.sort()
method sorts the elements of an array in ascending order. To sort the array in the order that you want, you can specify a custom Comparator
that compares the elements in the array and returns a negative value if the first element should come before the second element, a positive value if the first element should come after the second element, and 0 if the two elements are equal.
Here is an example of how you can use the Arrays.sort()
method with a custom Comparator
to sort the array of booleans:
import java.util.Arrays;
// Define a custom Comparator that compares two boolean values and returns a
// negative value if the first value is false and the second value is true,
// a positive value if the first value is true and the second value is false,
// and 0 if the two values are equal.
class BooleanComparator implements Comparator<Boolean> {
@Override
public int compare(Boolean a, Boolean b) {
if (a == b) {
return 0;
} else if (a == false && b == true) {
return -1;
} else {
return 1;
}
}
}
public class Main {
public static void main(String[] args) {
boolean[] arrayOfBoolean = ...; // initialize the array of booleans
// Sort the array of booleans using the BooleanComparator
Arrays.sort(arrayOfBoolean, new BooleanComparator());
}
}
In the above example, the BooleanComparator
class defines a compare()
method that compares two boolean values and returns a negative value if the first value is false and the second value is true, a positive value if the first value is true and the second value is false, and 0 if the two values are equal. This ensures that the Arrays.sort()
method will sort the array of booleans with false values coming before true values.