這個需要將代碼中的邏輯嵌入到Comparable類中。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MatchDescription implements Comparable<MatchDescription> {
private MatchType matchType;
private int matchOrder = 0;
private int subMatchOrder = 0;
public MatchDescription(String name) {
String matchName = name.toLowerCase().trim();
String[] tokens = matchName.split("\\s+");
matchType = MatchType.getByName(tokens[0]);
if (matchType.hasMatchOrder()) {
matchOrder = Integer.parseInt(tokens[1]);
}
if (matchType.hasSubmatches()) {
subMatchOrder = Integer.parseInt(tokens[3]);
}
}
現在,方法的compareTo(T)和equals(對象)正在使用的邏輯覆蓋的比賽順序,分階等
@Override
public int compareTo(MatchDescription other) {
if (this.matchType == other.matchType)
if (this.matchOrder == other.matchOrder)
return (this.subMatchOrder - other.subMatchOrder);
else
return (this.matchOrder - other.matchOrder);
else
return (this.matchType.getMatchTypeOrder() - other.matchType.getMatchTypeOrder());
}
@Override
public boolean equals(Object other) {
return (other instanceof MatchDescription) &&
(((MatchDescription) other).matchType == this.matchType) &&
(((MatchDescription) other).matchOrder == this.matchOrder);
}
匹配類型經由內枚舉管理:
private enum MatchType {
PIT("PIT", 1, false, false),
PREDICTIONS("PREDICTIONS", 2, false, false),
MATCH("Match", 3, true, false),
QUARTERS("Quarters", 4, true, true),
SEMIS("Semis", 5, true, true),
FINALS("Finals", 6, true, false)
;
private String name;
private int matchTypeOrder;
private boolean hasMatchOrder;
private boolean hasSubmatches;
MatchType(String name, int matchTypeOrder, boolean hasMatchOrder, boolean hasSubmatches) {
this.name = name;
this.matchTypeOrder = matchTypeOrder;
this.hasMatchOrder = hasMatchOrder;
this.hasSubmatches = hasSubmatches;
}
public boolean hasMatchOrder() {
return hasMatchOrder;
}
public boolean hasSubmatches() {
return hasSubmatches;
}
public static MatchType getByName(String matchName) {
for (MatchType value : values()) {
if (value.getName().equalsIgnoreCase(matchName))
return value;
}
return null;
}
private String getName() {
return name;
}
public int getMatchTypeOrder() {
return matchTypeOrder;
}
}
最後,的toString()給出了對象很好:
public String toString() {
String description = matchType.getName();
if (matchType.hasMatchOrder()) {
description += " " + matchOrder;
if (matchType.hasSubmatches())
description += " Match " + subMatchOrder;
}
return description;
}
這是一個關於如何使用可比對象的例子(注意空格也處理):
public static void main(String[] args) {
String[] inputs = new String[] {
"PIT ",
" Finals 1 ",
"PREDICTIONS ",
" Match 2 ",
" Quarters 1 Match 1 ",
" Quarters 1 Match 2 ",
" Match 1 ",
" Semis 1 Match 1 ",
" Semis 2 Match 1",
" Finals 2",
" Quarters 2 Match 1 "};
List<MatchDescription> c = new ArrayList<>();
for (String input : inputs) {
c.add(new MatchDescription(input));
}
Collections.sort(c);
for (MatchDescription e : c) {
System.out.println(e);
}
}
}
把所有規則放在'enum'我的客人 – Jerry06
請告訴我們你是怎麼稱呼這個的。 –
您可以指定0,1,2,3,4,5等小數字,但它不會有任何區別。實際的排序是如果有大量的項目需要花費時間,並且如果沒有大量的項目,考慮試圖改進該代碼是毫無意義的,因爲它將全部以大約納秒。 – EJP