Sort by reference type

Source: Internet
Author: User
Tags comparable

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.