Java custom Type comparison sort Comparator interface

Source: Internet
Author: User

String Service_time = " 6:00:00,7:00:00,8:00:00,9:00:00,10:00:00,11:00:00,12:00:00,13:00:00,14:00:00,15:00:00,16:00:00,17:00:00,18:00:00,19:00:00 , 20:00:00,21:00:00,22:00:00"; list<string> list = Stringutil.stringtolist (Service_time, ","new  stringtimesort ();  Collections.sort (List,sort);
 Packagecom.enation.eop.sdk.utils;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Comparator; Public classStringtimesortImplementscomparator{@Override Public intCompare (Object arg0, Object arg1) {//TODO auto-generated Method StubString TIME0 =(String) arg0; String time1=(String) arg1; SimpleDateFormat SDF=NewSimpleDateFormat ("HH:mm:ss"); intT0 = 0; intT1 = 0; Try{t0= (int) Sdf.parse (TIME0). GetTime (); T1= (int) Sdf.parse (time1). GetTime (); } Catch(ParseException e) {//TODO auto-generated Catch blockE.printstacktrace (); }                returnT0 > T1? 1:-1;//arranged by time from small to large    }}
Customizing sorting by implementing the comparator interface

Java custom Type comparison sort Comparator interface

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.