Arrays.sort(T[], Comparator < ? super T > c) method is used to sort object arrays by user-defined rules.
The official Java documentation only briefly describes the role of this method and does not introduce it in detail. This article will analyze this method in depth.
1. Simple example
The use of sort method is very simple and clear. In the following example, first define a Comparator that compares Dog size, and then pass its instance object as a parameter to the sort method. Through this example, you should be able to quickly master the usage of Arrays.sort().
import java.util.Arrays;import java.util.Comparator;class Dog{int size;public Dog(int s){size = s;}}class DogSizeComparator implements Comparator<Dog>{@Override public int compare(Dog o1, Dog o2) {return o1.size - o2.size;}}public class ArraySort {public static void main(String[] args) {Dog d1 = new Dog(2);Dog d2 = new Dog(1);Dog d3 = new Dog(3);Dog[] dogArray = {d1, d2, d3};printDogs(dogArray);Arrays.sort(dogArray, new DogSizeComparator());printDogs(dogArray);}public static void printDogs(Dog[] dogs){for (Dog d: dogs) System.out.print(d.size + " " );System.out.println();}}The output is:
2 1 3 1 2 3
2. Use the policy mode
This is a perfect and concise example of the Strategy Pattern. It is worth mentioning why the strategy model is suitable for use in this scenario.
Generally speaking, the policy pattern allows different algorithms to be selected when the program is executed. For example, when sorting, different comparators are passed in, and different algorithms are used.
According to the example above, suppose you want to sort by the weight of the Dog, you can create a new comparator to sort like below:
class Dog{int size;int weight;public Dog(int s, int w){size = s;weight = w;}}class DogSizeComparator implements Comparator<Dog>{@Override public int compare(Dog o1, Dog o2) {return o1.size - o2.size;}}class DogWeightComparator implements Comparator<Dog>{@Override public int compare(Dog o1, Dog o2) {return o1.weight - o2.weight;}}public class ArraySort {public static void main(String[] args) {Dog d1 = new Dog(2, 50);Dog d2 = new Dog(1, 30);Dog d3 = new Dog(3, 40);Dog[] dogArray = {d1, d2, d3};printDogs(dogArray);Arrays.sort(dogArray, new DogSizeComparator());printDogs(dogArray);Arrays.sort(dogArray, new DogWeightComparator());printDogs(dogArray);}public static void printDogs(Dog[] dogs){for (Dog d: dogs) System.out.print("size="+d.size + " weight=" + d.weight + " ");System.out.println();}}Execution results:
size=2 weight=50 size=1 weight=30 size=3 weight=40 size=1 weight=30 size=2 weight=50 size=3 weight=40 size=1 weight=30 size=3 weight=40 size=2 weight=50
Comparator is an interface, so the sort method can pass in any class that implements this interface, which is the main idea of the policy pattern.
3. Why use "super"
It is easy to understand if you use "Comparator<T>c", but the <?superT> in the second parameter of sort means that the type accepted by the comparator can be T or its superclass. Why is it a superclass? The answer is: This allows the comparison of different subclass objects using the same comparator. This is clearly demonstrated in the following example:
import java.util.Arrays;import java.util.Comparator;class Animal{int size;}class Dog extends Animal{public Dog(int s){size = s;}}class Cat extends Animal{public Cat(int s){size = s;}}class AnimalSizeComparator implements Comparator<Animal>{@Override public int compare(Animal o1, Animal o2) {return o1.size - o2.size;}//in this way, all sub classes of Animal can use this comparator.}public class ArraySort {public static void main(String[] args) {Dog d1 = new Dog(2);Dog d2 = new Dog(1);Dog d3 = new Dog(3);Dog[] dogArray = {d1, d2, d3};printDogs(dogArray);Arrays.sort(dogArray, new AnimalSizeComparator());printDogs(dogArray);System.out.println();//when you have an array of Cat, same Comparator can be used. Cat c1 = new Cat(2);Cat c2 = new Cat(1);Cat c3 = new Cat(3);Cat[] catArray = {c1, c2, c3};printDogs(catArray);Arrays.sort(catArray, new AnimalSizeComparator());printDogs(catArray);}public static void printDogs(Animal[] animals){for (Animal a: animals) System.out.print("size="+a.size + " ");System.out.println();}}Output result:
size=2 size=1 size=3 size=1 size=2 size=3 size=3 size=2 size=1 size=3 size=1 size=2 size=3
4. Summary
The information related to Arrays.sort() is summarized as follows:
General: super class strategy design pattern;
merge sort: time complexity n*log(n);
Java.util.Collections#sort(List < T > list, Comparator < ? super T > c) uses a similar idea to Arrays.sort.
Summarize
The above is all the detailed explanation of the Arrays.sort() code in Java. I hope it will be helpful to everyone. Interested friends can continue to refer to other Java-related topics on this website. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!