Java Collections's Sort API

Source: Internet
Author: User
Tags comparable

Here is the knowledge about the sort of Java collections

One:

Import Java.util.arraylist;import Java.util.collections;import Java.util.list;public class SortListDSemo2 {public static void Main (string[] args) {    list<point> list=new arraylist<point> ();    List.add (new Point (4,5));    List.add (new Point (2,4));    List.add (new Point (4,6));    List.add (new Point (9,6));    SYSTEM.OUT.PRINTLN (list);    /     * * Collections The Sor method requires that the sorted collection element must implement the comparable interface     * That interface is used to represent a method that can be compared to provide an abstraction Comparato used to define a comparison rule.     *      *    /Collections.sort (list);    SYSTEM.OUT.PRINTLN (list);}}

Two:

Import Java.util.arraylist;import java.util.collection;import java.util.collections;import java.util.Comparator; Import java.util.List; /** * Collections's Sort method has two disadvantages: * 1: Requires that our collection element must implement the comparable interface * This is a strong "intrusive" for our class; * 2: Sometimes the collection element implements the Comparale interface and defines the rule of comparison size * But the rules are not in the group we need for sorting * Collections provides an overloaded sort method: * static void sort (List list,comparable c) * This overloaded method requires us to pass in an extra comparator * according to the comparator's rules Compare elements in a collection natural sort * @author soft01 * */public class Demo1 {public static void main (string[] args) {list<string> List=ne W arraylist<string> (); List.add ("Xiaoming 16:19 05 sec"); List.add ("Dawn"); List.add ("No light in front of the window"); SYSTEM.OUT.PRINTLN (list);   Mycomparator com=new mycomparator ();   Collections.sort (list,com);   SYSTEM.OUT.PRINTLN (list);}   } Class Mycomparator implements comparator<string>{@Overridepublic int Compare (string O1, String O2) {return O1.leng Th ()-o2.length ();} }

Java Collections's Sort API

Related Article

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.