September 2019
Intermediate to advanced
816 pages
18h 47m
English
The built-in solution is named sort() and it comes in many different flavors in the java.util.Arrays class (15+ flavors).
Behind the sort() method, there is a performant sorting algorithm of the Quicksort type, named Dual-Pivot Quicksort.
Let's assume that we need to sort an array of integers by natural order (primitive int). For this, we can rely on Arrays.sort(int[] a), as in the following example:
int[] integers = new int[]{...};Arrays.sort(integers);
Sometimes, we need to sort an array of an object. Let's assume that we have a class as Melon:
public class Melon { private final String type; private final int weight; public Melon(String type, int weight) { this.type = type; this.weight = weight; } // getters omitted ...Read now
Unlock full access