Java sorting method sort Usage Details, sorting sort Usage Details

Source: Internet
Author: User

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.

  

  

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.