Sort an array:
Sorts the array public void ArraySort () {int[] arr = {1,4,6,333,8,2}; Arrays.sort (arr);//Use the Java.util.Arrays object's Sort method for (int i=0;i<arr.length;i++) {System.out.println (arr[i]);}}
To sort the collection:
Sorts 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 Collections's Sort method for (int a:list) {System.out.println (a);}} Sort list Descending 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 override the Compare method for (int a:list) {System.out.println (a);}}
Note: The sort method of collections is sorted by default in ascending order, and you need to override the Conpare method if you need to sort in descending order
Java sorting method How to use sort