(i) List and array
①list default Sort code:
public static void Main (string[] args) { list<string> List = new arraylist<> (); List.add ("A-li"); List.add ("No B Settling"); List.add ("Hee X Hee"); List.add ("One Y One"); List.add ("Pass C"); List.add ("East D West"); List.add ("E-blossoming ear"); List.add ("min f"); for (int i=0;i<list.size (); i++) { System.out.print (List.get (i)); } Descending "in the order of the existing list----output from back to forward" System.out.println ("descending"); Collections.reverse (list); for (int i=0;i<list.size (); i++) { System.out.print (List.get (i)); } According to the list now order---from going after reading a y one not B Andingdong D West C Recursive f Number of the X-ear e duo a Li System.out.println ("ascending"); Collections.sort (list); for (int i=0;i<list.size (); i++) { System.out.print (List.get (i)); } System.out.println ("in alphabetical order"); Sort ABCDEFG So come the "initials" Collections.sort (List,collator.getinstance (Locale.china)); for (int i=0;i<list.size (); i++) { System.out.print (List.get (i));} }
②: Array:
String[] names = {"A li", "Put B", "A Li", "D to", "Amount E", "D to", "a b li"};//should be based on the ASCII code so Arrays.sort (names); System.out.println (arrays.tostring (names)); Arrays.sort (Names,collator.getinstance (Locale.china));//According to the Chinese alphabet, first pinyin is compared with the first letter, then the second letter pinyin. System.out.println (arrays.tostring (names));
(ii) Custom sorting:
① implements the comparable interface, which compares items within the class.
public class Test1 implements comparable {/* * 1. Implement comparable interface * override int compareTo (Object o) method * */Private St ring name; Private Integer age; Public String GetName () {return name; } public void SetName (String name) {this.name = name; } public Integer Getage () {return age; public void Setage (Integer age) {this.age = age; } public Test1 (String name, Integer age) {this.name = name; This.age = age; } @Override public int compareTo (Object o) {Test1 sutdent = (Test1) o; int age1 = Sutdent.getage (); Return This.age.compareTo (AGE1); } public static void Main (string[] args) {list<test1> students = new arraylist<> (); Test1 S1 = new Test1 ("11", 11); Test1 s2 = new Test1 ("12", 12); Test1 s3 = new Test1 ("13", 13); Test1 S4 = new Test1 ("14", 14); Students.add (S1); Students.add (S4); Students.add (S2); Students.add (S3); Students.add (S2); Reverse only reverses collections.reverse (students); for (Test1 t:students) {System.out.print (T.getage () + ""); }//Call the Int property of your own class to compare collections.sort (students); for (Test1 t:students) {System.out.print (T.getage () + ""); } System.out.println (""); }}
② implements Comparator interface , class peripheral comparison items .
public class Test1 implements comparator<test1> {/* * 1. Implement comparable interface * override int compareTo (Object o) method * */ private String name; Private Integer age; Public String GetName () {return name; } public void SetName (String name) {this.name = name; } public Integer Getage () {return age; public void Setage (Integer age) {this.age = age; Public Test1 () {} public Test1 (String name, Integer age) {this.name = name; This.age = age; } @Override public int compare (Test1 O1, Test1 O2) {return o1.age.compareTo (o2.age); } public static void Main (string[] args) {Test1 s1=new Test1 ("Small 1", 11); Test1 s3=new Test1 ("Small 3", 13); Test1 s2=new Test1 ("Small 2", 12); Test1 s4=new Test1 ("Small 4", 14); Array.adlist is a fixed-length set. List<test1> students=arrays.aslist (S1,S3,S2,S4); for (Test1 s:students) {System.out.print (s.age+ ""); } System.out.println ("post-sorting"); Collections.sort (Students,new Test1 ()); for (Test1 s:students) {System.out.print (s.age+ ""); }
} }
Java sorting methods--list, arrays, "custom" inherit comparable and comparator