This article is applicable to abnormal time format, such as 2018-9-30 instead of 2018-09-30
As for why we should take a look at the compareTo method
The source code of compareTo is very simple:
It is to record the length of the two comparison values, take the minimum value of their length, and then compare the size in their minimum value length, convert them into char array, and return c1 - c2 in case of any difference in a character comparison. If they are the same, then return len1-len2;
public int compareTo(String anotherString) { int len1 = value.length; int len2 = anotherString.value.length; int lim = Math.min(len1, len2); char v1[] = value; char v2[] = anotherString.value; int k = 0; while (k < lim) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 - c2; } k++; } return len1 - len2; }
Generally, it can also be handled in this way, but for the time such as 2018-9-30 and 2018-10-30, when comparing the time of 9 and 1, 2018-9-30 will be regarded as a greater value.
So my approach is to convert the time of String type to the millisecond value of Long type, and sort them by millisecond value and bubbling.
private Long StringToLong(String str) throws ParseException { String a= str.replaceAll("/", "-"); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = df.parse(a); Long time = date.getTime(); return time; } ```take String Type of time converted to Long MS value of type ```java for(int i = 0 ; i < tFReport.size() ; i++) { for(int j = tFReport.size()-1 ; j > i ; j--) { try { Long a = StringToLong((tFReport.get(j).getAssayDate())); Long b = StringToLong((tFReport.get(j-1).getAssayDate())); if(a>b) { TFReport stu = tFReport.get(j); tFReport.set(j, tFReport.get(j-1)); tFReport.set(j-1, stu ); } } catch (ParseException e) { e.printStackTrace(); } } } ```Ranking method