COMPUTER SCIENCE AND ENGINEERING
ALGORITHMS
Question
[CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
|
|
They must be wrapper classes of primitives (Integer, Double, etc.)
|
|
They must be Comparable.
|
|
At least one instance variable must be a primitive.
|
|
They must be Strings
|
|
Objects are not sortable, only primitives are sortable.
|
Detailed explanation-1: -Sort an Object with Comparable To sort an Object by its property, you have to make the Object implement the Comparable interface and override the compareTo() method.
Detailed explanation-2: -Remember, though: if the object doesn’t implement Comparable, a ClassCastException will be thrown.
Detailed explanation-3: -Java Comparable interface is used to order the objects of the user-defined class. This interface is found in java. lang package and contains only one method named compareTo(Object). It provides a single sorting sequence only, i.e., you can sort the elements on the basis of single data member only.
Detailed explanation-4: -Comparator is used to sort an ArrayList of User-defined objects. In java, Comparator is provided in java. util package. Using Comparator we can sort ArrayList on the basis of multiple variables.