I want to state that every sorting in

Java collection framework is done in natural order, that is, in the ascending order. The sorting in Java is accomplished by two interfaces which are Comparable<T> and Comparator<T>. Comparable<T> lets us sort the collection based on only one item whereas the Comparator<T> lets us sort the collection based on multiple items.

Now, if any class wants to implement the Comparable<T> interface then it has to implement the following method. And when a class implements the Comparable<T> interface then the objects of that class will be mutually comparable with each other.

int compareTo(T o)

See, the return type of the above method is int, which has restricted the method to return a value which is within the following set of values: negative integer, zero, or positive integer. And based on the return value of this method after compare two mutually comparable objects, any sorting algorithm sorts the compared objects in natural order, that is, in ascending order. To clarify this, suppose the following Person class implements Comparable<T> interface:

class Person implements Comparable<Person> {

String firstName;

String lastName;

Person(String firstName, String lastName) {

this.firstName = firstName;

this.lastName = lastName;

}

public int compareTo(Person p) {

return this.firstName.compareTo(p.firstName);

}

}

Person unmesh = new Person(“Unmesh”, “Chowdhury”);

Person khokon = new Person(“Khokon”, “Chowdhury”);

Now, if any sorting algorithm calls unmesh.compareTo(khokon) then it will get a positive integer and sorts these two objects as follows: khokon, unmesh, what is in the ascending order according to the returned value.

Let me change the implementation of the compareTo(Person) method of the Person class as follows:

public int compareTo(Person p) {

return p.firstName.compareTo(this.firstName);

}

Now, suppose again the previous sorting algorithm calls unmesh.compareTo(khokon) then it will get a negative integer and sorts these two objects as follows: unmesh, khokon, what is in the ascending order according to the returned value.

First version of Person class:

Second version of Person class:

Now, Mr. Sahil Kapoor, come into your case. The output of the collection after sorting must be in the natural order or ascending order which is based on the implementation of the compareTo(T) method of the specific collection type.

In the case of list of Boolean; the Boolean class implementation of the compareTo(Boolean) method can be explained by the following example:

Suppose there are two Boolean objects,

Boolean b1 = new Boolean(true);

Boolean b2 = new Boolean(false);

• b1.compareTo(b2) will return positive integer

• b1.compareTo(b1) will return zero

• b2.compareTo(b2) will return zero

• b2.compareTo(b1) will return negative integer

Thus, in your case the output will be (ascending order):

false, false, true, true

In the case of list of Float; the Float class implements the compareTo(Float) in such a way that the sorting result will be in the following natural order or ascending order:

negative infinity

negative finite nonzero values

negative zero

positive zero

positive finite nonzero values

positive infinity

not a number (NaN)

Thus, in your case the output will be (ascending order):

-Infinity, -0.0, 0.0, Infinity, NaN