Java sorting method sort Usage Details, sorting sort Usage Details
Sort Arrays:
// Sort the array public void arraySort () {int [] arr = {6,333,}; Arrays. sort (arr); // use java. util. the sort method of the Arrays object for (int I = 0; I <arr. length; I ++) {System. out. println (arr [I]);}
Sort the set:
// Sort the list in ascending order. public void listSort1 () {List <Integer> list = new ArrayList <Integer> (); list. add (1); list. add (55); list. add (9); list. add (0); list. add (2); Collections. sort (list); // use the sort method of Collections for (int a: list) {System. out. println (a) ;}/// sort the list in descending order. public void listSort2 () {List <Integer> list = new ArrayList <Integer> (); list. add (1); list. add (55); list. add (9); list. add (0); list. add (2); Collections. sort (list, new Comparator <Integer> () {public int compare (Integer o1, Integer o2) {return o2-o1 ;}}); // use the sort method of Collections, and rewrite the compare method for (int a: list) {System. out. println ();}}
Note: The sort method of Collections is in ascending order by default. If you need to sort in descending order, you must override the conpare method.