There are two ways to sort in the Java tool class:
java.util.Collections.sort(java.util.List)
java.util.Collections.sort(java.util.List, java.util.Comparator)
package cn.mtianyan.sort; public class Cat implements Comparable{ private String name; //Name private int month; //Age private String species;//Varieties @Override public String toString() { return "[Full name:" + name + ", Age:" + month + ", Varieties:" + species + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public String getSpecies() { return species; } public void setSpecies(String species) { this.species = species; } public Cat(String name, int month, String species) { this.name = name; this.month = month; this.species = species; } @Override public int compareTo(Object o) { return this.month-((Cat)o).month; } }
package cn.mtianyan.sort; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class CatTest{ public static void main(String[] args) { List<Cat> list = new ArrayList<>(); // Define pet cat object Cat huahua = new Cat("tearful", 12, "british shorthair "); Cat fanfan = new Cat("Fan fan", 3, "Chinese garden cat"); Cat huahua02 = new Cat("Flower and flower two generations", 2, "british shorthair "); list.add(huahua); list.add(fanfan); list.add(huahua02); System.out.println("Pet cat information before sorting"); for (Cat cat: list) { System.out.println(cat); } // Perform sort operation Collections.sort(list); // Cat must implement the compatible interface and its methods System.out.println("Cat information sorted by age"); for (Cat cat: list) { System.out.println(cat); } } }
Use the Collection.sort method to sort. The sorted object must implement the Comparable interface and override the compareTo method
@Override public int compareTo(Object o) { return this.month-((Cat)o).month; }
Compares this object with the specified object for sorting. Returns a negative, zero, or positive integer that is less than, equal to, or greater than the specified object for the corresponding object.
For example, in the case of cats, the first 12 is compared with the second 3. 12-3 = 9 is greater than.
There is an error in the prompt below: it should be from small to large.

First name string sort
@Override public int compareTo(Object o) { return ((Cat)o).name.compareTo(this.name); }
// Define pet cat object Cat huahua = new Cat("aa", 12, "british shorthair "); Cat fanfan = new Cat("bb", 3, "Chinese garden cat"); Cat huahua02 = new Cat("cc", 2, "british shorthair ");
Operation result:

Collections.sort(List list, Comparator<T> comparator)
package cn.mtianyan.sort; public class CatTwo { private String name; //Name private int month; //Age private String species;//Varieties @Override public String toString() { return "[Full name:" + name + ", Age:" + month + ", Varieties:" + species + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public String getSpecies() { return species; } public void setSpecies(String species) { this.species = species; } public CatTwo(String name, int month, String species) { this.name = name; this.month = month; this.species = species; } }
package cn.mtianyan.sort; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class CatTwoTest { public static void main(String[] args) { List<CatTwo> list = new ArrayList<>(); // Define pet cat object CatTwo huahua = new CatTwo("aa", 12, "british shorthair "); CatTwo fanfan = new CatTwo("bb", 3, "Chinese garden cat"); CatTwo huahua02 = new CatTwo("cc", 2, "british shorthair "); list.add(huahua); list.add(fanfan); list.add(huahua02); System.out.println("Pet cat information before sorting"); for (CatTwo cat: list) { System.out.println(cat); } Comparator<CatTwo> comparator = new Comparator<CatTwo>() { @Override public int compare(CatTwo o1, CatTwo o2) { return o1.getMonth() - o2.getMonth(); } }; Collections.sort(list,comparator); System.out.println("Cat information after sorting(From small to large by month)"); for (CatTwo cat: list) { System.out.println(cat); } } }
Operation result:

String name from large to small
@Override public int compare(CatTwo o1, CatTwo o2) { // return o1.getMonth() - o2.getMonth(); return o2.getName().compareTo(o1.getName()); } }; Collections.sort(list,comparator); System.out.println("Cat information after sorting(From big to small by name)");
Operation result:

package cn.mtianyan.sort; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class SortArray { public static void main(String[] args) { Character [] charArray = new Character[]{'m','t','i','a','n','y','a','n'}; List<Character> list = Arrays.asList(charArray); System.out.println("====Character Before sorting===="); for (Character character:list) { System.out.print(character+" "); } System.out.println(); System.out.println("====Character After positive sequence===="); Collections.sort(list); for (Character character:list) { System.out.print(character+" "); } System.out.println(); System.out.println("====Character After reverse order===="); Collections.sort(list,Collections.reverseOrder()); for (Character character:list) { System.out.print(character+" "); } String [] stringArray = new String[]{"yes","ha","mtianyan","ab"}; System.out.println(); List<String> list1 = Arrays.asList(stringArray); System.out.println("====String Before sorting===="); for (String string:list1) { System.out.print(string+" "); } System.out.println(); Collections.sort(list1); System.out.println("====String After positive sequence===="); for (String string:list1) { System.out.print(string+" "); } Comparator cmp1 = Collections.reverseOrder(); Collections.sort(list1,cmp1); System.out.println(); System.out.println("====String After reverse order===="); for (String string:list1) { System.out.print(string+" "); } Integer[] intArray = new Integer[]{8,6,3,4,7,5,1}; List<Integer> list2 = Arrays.asList(intArray); System.out.println(); System.out.println("====Integer Before sorting===="); for (Integer integer:list2) { System.out.print(integer+" "); } System.out.println(); System.out.println("====Integer After positive sequence===="); Collections.sort(list2); for (Integer integer:list2) { System.out.print(integer+" "); } System.out.println(); System.out.println("====Integer After reverse order===="); Collections.sort(list2,Collections.reverseOrder()); for (Integer integer:list2) { System.out.print(integer+" "); } } }
Operation result:

package cn.mtianyan.sort; import java.util.HashSet; import java.util.Set; import java.util.concurrent.ConcurrentSkipListSet; public class SortedMap { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("ccc"); set.add("ddd"); set.add("eee"); set.add("aaa"); set.add("bbb"); for (String string:set){ System.out.println(string); } System.out.println("============="); ConcurrentSkipListSet<String> concurrentSkipListSet = new ConcurrentSkipListSet<>(); concurrentSkipListSet.add("ccc"); concurrentSkipListSet.add("ddd"); concurrentSkipListSet.add("eee"); concurrentSkipListSet.add("aaa"); concurrentSkipListSet.add("bbb"); for (String string:concurrentSkipListSet) { System.out.println(string); } } }
Operation result:

https://www.zhihu.com/question/28414001
Implementation class of SortedMap interface ConcurrentSkipListMap
We'll learn generics in the next section.