java Under normal circumstances, objects in can only be compared:==Or!=,out of commission>or<Yes, but in the development scenario, we need to sort multiple objects. By implication, we need to compare the size of objects,
There are two ways to sort objects in Java:
Method 1: natural sorting: Java lang.Comparable
The Comparable interface forces an overall ordering of the objects of each class that implements it. This sort is called the natural sort of classes
The class implementing Comparable must implement the compareTo(Object obj) method. The size of the two objects is compared through the return value of the compareTo(Object obj) method. If the current object this is greater than the parameter object obj, a positive integer is returned; if the current object this is less than the parameter object obj, a negative integer is returned; if the current object this is equal to the parameter object obj, zero is returned.
The list of objects (and arrays) that implement the Comparable interface can be automatically sorted through Collections.sort or Arrays.sort. Objects that implement this interface can be used as keys in ordered mappings or elements in ordered collections without specifying comparators.
For each e1 and e2 of class C, if and only if e1 CompareTo (e2) = = 0 and e1 When equals (e2) has the same boolean value, the natural sorting of class C is called consistent with equals. It is recommended (although not required) that it is best to make the natural ranking consistent with equals.
Typical implementation of Comparable: (by default, they are arranged from small to large)
String: compare according to the Unicode value of the characters in the string
Character: compares characters according to their Unicode values
Wrapper classes corresponding to numeric types and BigInteger and BigDecimal: compare them according to their corresponding numeric size. Boolean: the wrapper class instance corresponding to true is greater than that corresponding to false
Date, Time, etc.: the later date and Time is greater than the previous date and Time
String implements the test of Comparable interface
public void test1(){ String[] arr=new String[]{"AA","BB","FF","CC","DD"}; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); }
Example 1:
class Goods implements Comparable { private String name; private double price; //Compare the size of the goods according to the price @Override public int compareTo(Object o) { if(o instanceof Goods) { Goods other = (Goods) o; //Mode 1 if (this.price > other.price) { return 1; } else if (this.price < other.price) { return -1; } return 0; //Mode II return Double.compare(this.price,goods.price); //The wrapper class itself implements the Comparable interface } throw new RuntimeException("The data type entered is inconsistent"); } //Constructor, getter, setter, toString() strategy } public class ComparableTest{ public static void main(String[] args) { Goods[] all = new Goods[4]; all[0] = new Goods("<A dream of Red Mansions", 100); all[1] = new Goods("<Journey to the West", 80); all[2] = new Goods("<Romance of the Three Kingdoms", 140); all[3] = new Goods("<Outlaws of the marsh", 120); Arrays.sort(all); System.out.println(Arrays.toString(all)); } }
Method 2: custom sorting: Java util. Comparator
When the element type does not implement Java Lang. comparable interface and inconvenient to modify code, or implement Java The sorting rules of the lang. comparable interface are not suitable for the current operation, so you can consider using the Comparator object to sort and force the overall sorting comparison of multiple objects
Override the compare(Object o1,Object o2) method to compare the sizes of o1 and o2: if the method returns a positive integer, o1 is greater than o2; If 0 is returned, it means equal; Returns a negative integer indicating that o1 is less than o2.
Comparator s can be passed to sort methods, such as Collections.sort or Arrays.sort, to allow precise control over sort order.
You can also use Comparator to control the order of some data structures (such as ordered set or ordered mapping), or to provide sorting for object collection without natural order.
Example 1:
public void test3(){ String[] arr=new String[]{"aa","cc","gg","bb"}; Arrays.sort(arr,new Comparator() { @Override public int compare(Object o1, Object o2) { if(o1 instanceof String&&o2 instanceof String ){ String s1=(String)o1; String s2=(String)o2; return -s1.compareTo(s2); } throw new RuntimeException("The data type entered is inconsistent"); } }); System.out.println(Arrays.toString(arr)); }
Example 2:
Goods[] all = new Goods[4]; all[0] = new Goods("War and Peace", 100); all[1] = new Goods("Childhood", 80); all[2] = new Goods("Scarlet and Black", 140); all[3] = new Goods("Notre Dame de Paris", 120); Arrays.sort(all, new Comparator() { @Override public int compare(Object o1, Object o2) { if(o1 instanceof Goods&&o2 instanceof Goods){ Goods g1 = (Goods) o1; Goods g2 = (Goods) o2; return g1.getName().compareTo(g2.getName()); } } }); System.out.println(Arrays.toString(all));
2, Comparison of Cmparable and Comparator
Comparable Once the interface mode is certain, ensure comparable The object of the interface implementation class can be compared in size anywhere comparator The interface is temporary