Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does arrayListName.sort(null) do?

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.

like image 468
new2code Avatar asked Mar 22 '18 18:03

new2code


People also ask

How do you sort an array with NULL values coming last?

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.

How to find the position of a null in an 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.

How to sort values in the descending order but nulls come first?

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.

How do you sort Nulls in SQLite?

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.


2 Answers

Explanation

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 the Comparable 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.


Details

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.

like image 129
Zabuzard Avatar answered Oct 28 '22 23:10

Zabuzard


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.

like image 2
Matt Avatar answered Oct 29 '22 00:10

Matt