160308, Java sort (shape like 1.1, 1.2.1)

Source: Internet
Author: User

Package com.rick.sample; Import Java.util.arraylist;import java.util.collections;import java.util.comparator;import java.util.List; public class Sortutildemo {Public static void Sortidentify (List<string> lists) {sortcomparator sortcomparator = new Sortcomparator ();collections.sort (lists, sortcomparator); }Public static void Main (string[] args) {arraylist<string> lists = new arraylist<string> ();Lists.add ("1.1");Lists.add ("1.2");Lists.add ("1.2.1");Lists.add ("3.1.2");Lists.add ("2.6.6");Lists.add ("2.6.3");Lists.add ("1.10.1");Lists.add ("1.10.2");Lists.add ("1.10.1.2");Lists.add ("1.1.1");Lists.add ("10.1.1");sortidentify (lists);For (String ids:lists) {System.out.println (IDS); } }}/** * Override sort interface * @author Administrator */class Sortcomparator implements comparator<string>{@Overridepublic int Compare (string pre, String suf) {string[] Prearrs = pre.split ("\ \");string[] Sufarrs = suf.split ("\ \");int len = prearrs.length > sufarrs.length? preArrs.length:sufArrs.length;for (int i = 0; i < len; i++) {int prenum = 0;int sufnum = 0;if (i < prearrs.length) {prenum = Integer.parseint (Prearrs[i]); }if (i <sufarrs.length) {sufnum = Integer.parseint (Sufarrs[i]); }if (Prenum > Sufnum) {return 1;} else if (Prenum < sufnum) {return-1; }  }return 0; }}

160308, Java sort (shape like 1.1, 1.2.1)

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.