1. Built-in class:
A) integer, decimal integer,float,double directly compare the size of the base data type
b) Character: Compare the difference between Unicode codes
c) String: If one is a substring of another starting start, returns the difference in length, otherwise the difference of the first unequal Unicode code is returned
D) Java.util.Date: Comparison of long-shaped numbers based on dates
String Ordering:
Importjava.util.Arrays; Public classDemo02 { Public Static voidMain (string[] args) {string[] arr= {"A", "abc", "Def", "ABCD"}; Booleansorted =true; for(intj = 0; j<arr.length-1; J + +) {//Number of Tripssorted =true;//assumed orderSystem.out.println ("j+1" + "Trip")); for(inti=0; i<arr.length-1-j; i++) {//Number of comparisonsSystem.out.print ("i+1" + "secondary")); if(((comparable) arr[i]). CompareTo (arr[i+1]) > 0) {String temp=Arr[i]; Arr[i]= Arr[i+1]; Arr[i+1] =temp; Sorted=false;//unordered} System.out.println (Arrays.tostring (arr)); } if(sorted) {//if ordered, reduce the number of trips Break; }} System.out.println (Arrays.tostring (arr)); }}
Date Sort:
Importjava.util.Arrays;Importjava.util.Date; Public classDemo03 { Public Static voidMain (string[] args) {date[] arr=NewDate[3]; arr[0] =NewDate (); arr[1] =NewDate (System.currenttimemillis ()-1000*60*60); arr[2] =NewDate (System.currenttimemillis () + 1000*60*60); Booleansorted =true; for(intj = 0; j<arr.length-1; J + +) {//Number of Tripssorted =true;//assumed orderSystem.out.println ("j+1" + "Trip")); for(inti=0; i<arr.length-1-j; i++) {//Number of comparisonsSystem.out.print ("i+1" + "secondary")); if(((comparable) arr[i]). CompareTo (arr[i+1]) > 0) {Date temp=Arr[i]; Arr[i]= Arr[i+1]; Arr[i+1] =temp; Sorted=false;//unordered} System.out.println (Arrays.tostring (arr)); } if(sorted) {//if ordered, reduce the number of trips Break; }} System.out.println (Arrays.tostring (arr)); }}
Container sorting:
Importjava.util.Arrays;Importjava.util.List; Public classUtils {//List Sort Public Static<textendsComparable<t>>voidSort (list<t>list) {object[] arr=List.toarray (); Sort (arr); for(inti=0; i<arr.length-1; i++) {List.set (I, (T) arr[i]); } } /*** Sorting of arrays (descending) *@paramarr*/ Public Static voidsort (object[] arr) {//sort descending from large to small BooleanSorted=true; intLen =arr.length; for(intj=0;j<len-1;j++) {//Number of Tripssorted =true;//assumed order for(inti=0;i<len-1-j;i++) {//number of if(((comparable) arr[i]). CompareTo (arr[i+1]) <0) {Object temp=Arr[i]; Arr[i]=arr[i+1]; Arr[i+1] =temp; Sorted=false;//Assuming failure } } if(sorted) {//reduce the number of trips Break; } } } //array ordering using generics Public Static<textendsComparable<t>>voidsort (t[] arr) {Booleansorted =true; for(intj = 0; j<arr.length-1; J + +) {//Number of Tripssorted =true;//assumed orderSystem.out.println ("j+1" + "Trip")); for(inti=0; i<arr.length-1-j; i++) {//Number of comparisonsSystem.out.print ("i+1" + "secondary")); if(((comparable) arr[i]). CompareTo (arr[i+1]) > 0) {T temp=Arr[i]; Arr[i]= Arr[i+1]; Arr[i+1] =temp; Sorted=false;//unordered} System.out.println (Arrays.tostring (arr)); } if(sorted) {//if ordered, reduce the number of trips Break; } } }}
Sort by reference type