I have a project and the professor gave us some code. There is a line in the code that has me confused:
arrayListName.sort(null);
what does the call to sort(null)
do exactly?
The doc says: "If the specified comparator is null then all elements in this list must implement the Comparable interface and the elements' natural ordering should be used. This list must be modifiable, but need not be resizable." What does it mean by the list's natural ordering? The elements we are trying to sort are phone numbers.
Note: I read the javadoc and it is not clear to me what it means. English is not my first language and the professor does not teach the class in English. I tried to google the question but am still confused as to what it means specifically.
Call the sort () method on the array, passing it a function. The function defines the sort order. Sort the null values at the end. We used the Array.sort method to sort an array with the null values coming last. Note that the sort () method sorts the elements of the array in place and returns the sorted array.
// // If b is null, it must therefore be placed closer to whichever end the null // elements should end up on. If ascending, null elements are pulled towards // the right end of the array. If descending, null elements are pulled towards // the left. // // Therefore, we return -nullPosition.
To sort values in the descending order but with NULLs coming first, we can use the following query in MySQL: The query will result in the output being ordered by the year column in descending order. Here, the NULLs appear first – the same result we get with the NULLS FIRST option in SQLite. Using the IS (NOT) NULL operator.
In contrast to PostgreSQL and Oracle, SQLite treats NULLs as very small values and puts them first in an ascending sort and last in a descending sort. Starting with SQLite version 3.30.0, this behavior can also be easily changed using the NULLS FIRST / NULLS LAST option.
Supposed arrayListName
is actually a variable of type ArrayList
, then you are calling the List#sort
method here. From its documentation:
default void sort(Comparator<? super E> c)
Sorts this list according to the order induced by the specified
Comparator
.If the specified comparator is
null
then all elements in this list must implement theComparable
interface and the elements' natural ordering should be used.
So the method uses the natural ordering of elements when the comparator is null
.
This natural ordering is given by a compareTo
method on the items when they implement the interface Compareable
(documentation). For int
this sorts increasing. For String
this sorts based on the lexicographical order.
Examples after sorting with natural ordering:
1, 2, 3, 3, 3, 8, 11
"A", "B", "H", "Helicopter", "Hello", "Tree"
Many classes implement this interface already. Take a look at the documentation. It counts 287
classes currently.
Let's compare that to the actual implementation:
@Override
@SuppressWarnings("unchecked")
public void sort(Comparator<? super E> c) {
final int expectedModCount = modCount;
Arrays.sort((E[]) elementData, 0, size, c);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
The comparator c
is passed to the method Arrays#sort
, let's take a look at an excerpt from its implementation:
if (c == null) {
sort(a, fromIndex, toIndex);
}
We follow that call to another Arrays#sort
method (implementation). This method sorts elements based on their natural ordering. So no comparator is used.
I typed it up and it did not throw an error. It actually sorted the list correctly. This is my code:
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(2);
nums.add(4);
nums.add(3);
nums.add(1);
System.out.println(nums);
nums.sort(null);
System.out.println(nums);
The output was:
[2, 4, 3, 1]
[1, 2, 3, 4]
The sort method accepts a Comparator object, and if null is passed it defaults to natural ordering.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With