In Java, if you want to sort the collection objects or array objects, you need to achieve the Comparator interface to achieve the goal we want.
Next we simulate the date attributes in the collection object
1. STEP of the physical class
package com.ljq.entity;/*** transport number single process** @AutHor Administrator**/Public Class Step {/** processing time*/private string acceptTime = "";/** Express. private String acceptAddress = "" "; Public Step () {Super ();} Public Step (String Accepttime, String AcceptAddress) {Super (); This.accepttime = accep TTIME; this.Acceptaddress = AcceptAddress;} Public String GetAccepttime () {Return AcceptTime;} Public Void SetacCeptTime (String AcceptTime) {this.accepttime = accepttime;} Public StringAcceptadDress () {Return a CCEPTADDDRESS;} Public Void Setac findaddress (String AcceptAddress) {this.acceptaddress = AcceptAddress;}}}
2. Implement the Comparator interface
package com.ljq.entity; Import java.util.comparators; Import java.util.date; Import com.ljq.utilTool; ATOR * */Public Class Stepcomparators Implements Comparator <step> { / ** * If O1 is less than O2, returns a negative number; if O1 is greater than O2, returns a positive number; if they are equal, return 0; * / @Override Public Int Compare (STEP O1, STEP O2, STEP O2, STEP O2, STEP O2, STEP O2 ) {Date accepttime1 = UtilTool.Streodate (O1.getAccepttime (), Null); date accettime2 = UtilTool.Streplodate (O2.GetacCEPTTIME (), Null); / /Sequence the date field, if you want (Accepttime1.after (Accepttime2)) Return 1; Return -1;}}
Third, test
package junit; Import Java.util.Collection; Import Java.util.Collections; Import Java.util.List; Import Org.junit.test; Public Class Atortest {@Test Public Void Sort () Throws Exception {list <step> Steps = New ArrayList <tep>; // Sort the collection objects Stepcomparator Comparators = New Stepcomparators (); Collections.sort (Steps, Comparators); if (Steps! = NULL && STEPS.Size ( )> 0) {for (Step Step: Steps) {system.out.println (step.getAcceptadDress ()); system.out.println (step.getaccepttime ());}}}}}}}